首页 > 编程语言 > 详细

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

时间:2017-08-19 13:44:14      阅读:3440      评论:0      收藏:0      [点我收藏+]

需要用到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)
    

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

原文:http://www.cnblogs.com/luhouxiang/p/7396239.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!