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

前端 未结 6 1221
孤街浪徒
孤街浪徒 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

    if the problem is that we want DATEVALUE() excel serial number for dates, the toordinal() function can be used. Python serial numbers start from Jan1 of year 1 whereas excel starts from 1 Jan 1900 so apply an offset. Also see excel 1900 leap year bug (https://support.microsoft.com/en-us/help/214326/excel-incorrectly-assumes-that-the-year-1900-is-a-leap-year)

    def convert_date_to_excel_ordinal(day, month, year) :
    
        offset = 693594
        current = date(year,month,day)
        n = current.toordinal()
        return (n - offset)
    

提交回复
热议问题