Time

Time

例子:

时间戳格式:1500000000
结构化格式:元组(结构化):struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天等)  
结构化格式:time.struct_time(tm_year=2017, tm_mon=8, tm_mday=8, tm_hour=18, tm_min=54, tm_sec=21, tm_wday=1, tm_yday=220, tm_isdst=0)

时间字符串:
170808 065158 Tuesday August 对应 print(time.strftime('%y%m%d %I%M%S %A %B'))
小结:时间戳是计算机能够识别的时间;时间字符串是人能够看懂的时间;元组则是用来操作时间的

把结构化转字符串,精确到日
def ts2HMS(ts):
return time.strftime('%Y-%m-%d', time.localtime(ts))  #time.strftime(结构化) 把结构化转字符串
把结构化转时间戳 def HMS2ts(date):
return int(time.mktime(time.strptime(date, '%Y-%m-%d')))  #time.mktime(结构化) 把里面的结构化转时间戳 time.strptime(字符串) 把里面的字符串转结构化
把结构化转字符串,精确到秒 def ts2HMS_1(ts):
return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(ts)) #time.localtime(时间戳) 把里面的时间戳转结构化
def check_main(start,end,DAY_NUM):
'''
输入数据:
start:开始日期,格式“年-月-日”
end:结束日期,格式“年-月-日”
'''
start_time = time.time()
day = (HMS2ts(end)-HMS2ts(start))/(24*60*60)
   print day #day=26
if __name__ == '__main__':
#read_mongo()
start = '2018-05-01'
end = '2018-05-27'

 

posted @ 2018-05-29 15:44  Aray007  阅读(131)  评论(0编辑  收藏  举报