需要用到datetime,将datetime结构中的年,月,日,时,分,秒分别取出,乘上对应的整数即可。顺便说一下,由于python中int型是64位,因此可将之一并表达,不会出现C++中可能超过32位的问题
上代码:
# /usr/bin/python3# -*- encoding: utf-8 -*-‘‘‘测试时间转换‘‘‘import datetimedef convert_date_to_int(dt):t = dt.year * 10000 + dt.month * 100 + dt.dayt *= 1000000return tdef convert_dt_to_int(dt):t = convert_date_to_int(dt)t += dt.hour * 10000 + dt.minute * 100 + dt.secondreturn tif __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)
时间: 2024-11-06 16:37:20