python時間轉換(時間轉成int)
阿新 • • 發佈:2017-08-19
spa 時間 turn ack otto mon 問題 usr minute
需要用到datetime,將datetime結構中的年,月,日,時,分,秒分別取出,乘上對應的整數即可。順便說一下,由於python中int型是64位,因此可將之一並表達,不會出現C++中可能超過32位的問題
上代碼:
# /usr/bin/python3# -*- encoding: utf-8 -*-‘‘‘測試時間轉換‘‘‘import datetime
def convert_date_to_int(dt):
t = dt.year * 10000 + dt.month * 100 + dt.dayt *= 1000000return t
def convert_dt_to_int(dt):
t = convert_date_to_int(dt)t += dt.hour * 10000 + dt.minute * 100 + dt.secondreturn t
if __name__=="__main__":date = datetime.date(2017,8,19)idate = convert_date_to_int(date)print("date: ", idate)date_time = datetime.datetime(2017,8,19,12,13,30)idate_time = convert_dt_to_int(date_time)print("date_time: ", idate_time)
python時間轉換(時間轉成int)