Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a python datetime.datetime to excel serial date number

I need to convert dates into Excel serial numbers for a data munging script I am writing. By playing with dates in my OpenOffice Calc workbook, I was able to deduce that '1-Jan 1899 00:00:00' maps to the number zero.

I wrote the following function to convert from a python datetime object into 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 some sample dates, the numbers are different from those I get when I format the date as a number in Excel (well OpenOffice Calc). For example, testing '2009-03-20' gives 3478032000 in Python, whilst excel renders the serial number as 39892.

What is wrong with the formula above?

*Note: I am using Python 2.6.3, so do not have access to datetime.total_seconds()

like image 513
Homunculus Reticulli Avatar asked Mar 05 '12 22:03

Homunculus Reticulli


People also ask

How do I convert a date to a serial number in Python?

By using xlrd. xldate_as_datetime() function this can be achieved. The xlrd. xldate_as_datetime() function is used to convert excel date/time number to datetime.

How do I convert a date to a serial number in Excel?

Convert date to serial number with DATEVALUESelect a blank cell which will place the serial number, type this formula =DATEVALUE("8/8/2017"), press Enter key. Now the date in formula will be displayed as serial number.

How do I convert a date in Excel to a number in Python?

The Excel “serial date” format is actually the number of days since 1900-01-00. The strftime() function is used to convert date and time objects to their string representation. It takes one or more inputs of formatted code and returns the string representation.


2 Answers

It appears that the Excel "serial date" format is actually the number of days since 1900-01-00, with a fractional component that's a fraction of a day, based on http://www.cpearson.com/excel/datetime.htm. (I guess that date should actually be considered 1899-12-31, since there's no such thing as a 0th day of a month)

So, it seems like it should be:

def excel_date(date1):     temp = dt.datetime(1899, 12, 30)    # Note, not 31st Dec but 30th!     delta = date1 - temp     return float(delta.days) + (float(delta.seconds) / 86400) 
like image 140
akgood Avatar answered Oct 01 '22 03:10

akgood


While this is not exactly relevant to the excel serial date format, this was the top hit for exporting python date time to Excel. What I have found particularly useful and simple is to just export using strftime.

import datetime current_datetime = datetime.datetime.now() current_datetime.strftime('%x %X') 

This will output in the following format '06/25/14 09:59:29' which is accepted by Excel as a valid date/time and allows for sorting in Excel.

like image 40
JazzyWhit Avatar answered Oct 01 '22 03:10

JazzyWhit