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

前端 未结 6 1227
孤街浪徒
孤街浪徒 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 17:02

    According to @akgood's answer, when the datetime is before 1/0/1900, the return value is wrong, the corrected return expression may be:

    def excel_date(date1):
        temp = dt.datetime(1899, 12, 30)    # Note, not 31st Dec but 30th!
        delta = date1 - temp
        return float(delta.days) + (-1.0 if delta.days < 0 else 1.0)*(delta.seconds)) / 86400
    

提交回复
热议问题