python时间转换(时间转成int)

需要用到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.day
    t *= 1000000
    return t
def convert_dt_to_int(dt):
    t = convert_date_to_int(dt)
    t += dt.hour * 10000 + dt.minute * 100 + dt.second
    return 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)
    
文章来自:http://www.cnblogs.com/luhouxiang/p/7396239.html
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3