2015-07-11 159 views

回答

4
from datetime import datetime 
excel_date = 42139 
dt = datetime.fromordinal(datetime(1900, 1, 1).toordinal() + excel_date - 2) 
tt = dt.timetuple() 
print dt 
print tt 

正如JF塞巴斯蒂安提到,這個答案只適用於任何日期1900年3月1日

編輯後:(在回答@RK)

如果您excel_date是浮數,使用此代碼:

def floatHourToTime(fh): 
    h, r = divmod(fh, 1) 
    m, r = divmod(r*60, 1) 
    return (
     int(h), 
     int(m), 
     int(r*60), 
    ) 

excel_date = 42139.23213 
dt = datetime.fromordinal(datetime(1900, 1, 1).toordinal() + int(excel_date) - 2) 
hour, minute, second = floatHourToTime(excel_date % 1) 
dt = dt.replace(hour=hour, minute=minute, second=second) 
+0

它的工作,但迄今爲止42139即將爲2015年5月17日00:00:00還不如2015年5月15日00:00:00 – user2728024

+0

海峽憤怒,你會檢查'-2'是否解決了嗎? – saeedgnu

+0

是的。 -2解決了它。但很奇怪。 – user2728024