I need to convert dates to Excel serial numbers for data collection script I am writing. Playing with the dates in my OpenOffice Calc book, I was able to deduce that "Jan 1, 1899 00:00:00" displays a zero number.
I wrote the following function to convert from a python datetime object to an Excel serial number:
def excel_date(date1): temp=dt.datetime.strptime('18990101', '%Y%m%d') delta=date1-temp total_seconds = delta.days * 86400 + delta.seconds return total_seconds
However, when I try a few approximate dates, the numbers are different from the ones I get when I format the date as a number in Excel (well OpenOffice Calc). For example, testing “2009-03-20” yields 3478032000 in Python, while Excel allocates serial number 39892.
What is wrong with the formula above?
* Note. I am using Python 2.6.3, so I do not have access to datetime.total_seconds ()
python datetime excel data-munging
Homunculus reticulli
source share