python3 时间戳和时间的一些转化

1.时间日期转时间戳

import datetime
now = datetime.datetime.now()#datatime类型
time_stamp = time.mktime(now.timetuple())
print(int(time_stamp))#秒级

2.时间字符串转时间戳

st = time.strptime('2018-10-07 16:12:54', '%Y-%m-%d %H:%M:%S')
time_stamp = time.mktime(st) 
print(int(time_stamp))#秒级

3.直接生成当前时间戳

import time
time_stamp = int(time.time())
print(time_stamp)#int

4.时间戳转时间字符串

ltime = time.localtime(1552723974)
time_stamp  = time.strftime('%Y-%m-%d %H:%M:%S',ltime)
print(time_stamp)#str

 

posted @ 2019-03-16 15:49  那时的吻狠陶醉  阅读(1244)  评论(0编辑  收藏  举报