pythondatedatetimefractionsdate-manipulation

How to get the next date which is a fraction year away from the given date?


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.


Solution

  • 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.