1

I have a datetime object called dt and I want to get the next date which 0.93 years away from it. I was trying relativedelta but it seems that the function cannot take fractional years.

dt = datetime.datetime(2012, 4, 30)
dt + relativedelta(years = 0.93)

>> ValueError: Non-integer years and months are ambiguous and not currently supported.

Any help is appreciated.

2 Answers2

3

relativedelta doesn't support fractions. Easiest way to do this would be to convert the fraction to seconds and use that. e.g,

YEAR_SECONDS = 60 * 60 * 24 * 365
dt + relativedelta(seconds = int(0.93 * YEAR_SECONDS))
Hari Menon
  • 33,649
  • 14
  • 85
  • 108
1

Try multiplying the float no by the number of days in a year. This will give you 0.93 * 365 days after the current date.

dt = datetime.datetime.today() + datetime.timedelta(days=int(365*.93))

OUTPUT:-

2020-05-09 08:40:28.507170

NOTE:-

In the above process we are converting into int, because the date is needed. If some more precise time duration (i.e Hours, Minutes etc) is needed, then this process may not be the best.

Vasu Deo.S
  • 1,820
  • 1
  • 11
  • 23