I have been using a function to convert datetime objects to decimal years based on this post python how to convert datetime dates to decimal years but I was unable to find any post showing the inverse: decimal years to datetime objects. Any help in this regard is appreciated.
thanks,
Not properly tested, but looks about right:
from datetime import datetime, timedelta
start = 2012.5
year = int(start)
rem = start - year
base = datetime(year, 1, 1)
result = base + timedelta(seconds=(base.replace(year=base.year + 1) - base).total_seconds() * rem)
# 2012-07-02 00:00:00
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With