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

前端 未结 6 1211
孤街浪徒
孤街浪徒 2020-11-27 16:01

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

6条回答
  •  暖寄归人
    2020-11-27 16:43

    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)
    

提交回复
热议问题