如何将给定的序号(从Excel)转换为日期 [英] How to convert a given ordinal number (from Excel) to a date
问题描述
如果在excel中使用这个数字,然后右键单击并格式化单元格,那么该值将被转换为04/06/2004,并且我需要使用python相同的结果。如何实现这个
Excel中的偏移量从1900/01/01;添加天数作为timedelta:
from datetime import date,timedelta
def from_excel_ordinal(序数,_epoch =日期(1900,1,1)):
如果ordinal> 59:
ordinal - = 1#Excel闰年错误,1900年不是闰年!
return _epoch + timedelta(days = ordinal - 1)#epoch is day 1
I不得不在1900/02/28之后的任何日期调整序号 2 Excel从Lotus 1-2-3继承了闰年错误,并处理了1900作为一个闰年。上述代码为 59
和 60
日期(1900,2,28)
/ code>以更正此问题。
I have a Value 38142 I need to convert it into date format using python. if use this number in excel and do a right click and format cell at that time the value will be converted to 04/06/2004 and I need the same result using python. How can I achieve this
The offset in Excel in from 1900/01/01; add the number of days as a timedelta:
from datetime import date, timedelta
def from_excel_ordinal(ordinal, _epoch=date(1900, 1, 1)):
if ordinal > 59:
ordinal -= 1 # Excel leap year bug, 1900 is not a leap year!
return _epoch + timedelta(days=ordinal - 1) # epoch is day 1
I had to adjust the ordinal by 2 for any date after 1900/02/28; Excel has inherited a leap year bug from Lotus 1-2-3 and treats 1900 as a leap year. The code above returns date(1900, 2, 28)
for both 59
and 60
to correct for this.
这篇关于如何将给定的序号(从Excel)转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!