How to convert Python datetime dates to decimal/float years
newbedev.com › how-to-convert-python-datetimeHow to convert Python datetime dates to decimal/float years. from datetime import datetime as dt import time def toYearFraction (date): def sinceEpoch (date): # returns seconds since epoch return time.mktime (date.timetuple ()) s = sinceEpoch year = date.year startOfThisYear = dt (year=year, month=1, day=1) startOfNextYear = dt (year=year+1, month=1, day=1) yearElapsed = s (date) - s (startOfThisYear) yearDuration = s (startOfNextYear) - s (startOfThisYear) fraction = ...
Converting a timedelta to a float - Make More Machines
agapow.net › python › convert-timedelta-to-floatWhen you subtract a datetime from another in Python, you get a timedelta object: > from datetime import datetime, timedelta > d1 = datetime (year=2015, month=6, day=1, hour=12, minute=30) > d2 = datetime (year=2015, month=6, day=7, hour=18, minute=15) > td = d2 - d1 > td datetime.timedelta (6, 20700) > type (td) datetime.timedelta. Unfortunately, timedelta only returns the "parts" that make it up - days, seconds and microseconds: