Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

decimal years to datetime in python

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,

like image 220
hrishi Avatar asked Jan 03 '14 19:01

hrishi


1 Answers

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
like image 92
Jon Clements Avatar answered Sep 17 '22 16:09

Jon Clements