标签:
举例:
1 #-*-coding=utf-8-*- 2 __author__=‘zhongtang‘ 3 4 5 import time 6 strtime1=‘20160518010101‘ 7 strtime2=‘20160518020101‘ 8 9 #字符串变成时间数据结构 10 localtime1=time.strptime(strtime1,‘%Y%m%d%H%M%S‘) 11 localtime2=time.strptime(strtime2,‘%Y%m%d%H%M%S‘) 12 13 print type(localtime1),localtime1 14 print type(localtime2),localtime2 15 16 17 #从时间数据结构转换成时间戳 18 time1= time.mktime(localtime1) 19 time2= time.mktime(localtime2) 20 21 print type(time1),time1 22 print type(time2),time2 23 24 #时间戳可以直接相减,得到以秒为单位的差额 25 print time2-time1
输出结果
1 <type ‘time.struct_time‘> time.struct_time(tm_year=2016, tm_mon=5, tm_mday=18, tm_hour=1, tm_min=1, tm_sec=1, tm_wday=2, tm_yday=139, tm_isdst=-1) 2 <type ‘time.struct_time‘> time.struct_time(tm_year=2016, tm_mon=5, tm_mday=18, tm_hour=2, tm_min=1, tm_sec=1, tm_wday=2, tm_yday=139, tm_isdst=-1) 3 <type ‘float‘> 1463504461.0 4 <type ‘float‘> 1463508061.0 5 3600.0
标签:
原文地址:http://www.cnblogs.com/zhongtang/p/5507035.html