欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

时间戳

程序员文章站 2022-07-03 15:00:56
...
import time
from datetime import datetime

now = datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S")
timeArray = time.strptime(now, "%Y-%m-%d %H:%M:%S")
print(timeArray, type(timeArray))
# time.struct_time(tm_year=2019, tm_mon=2, tm_mday=23, tm_hour=15, tm_min=26, tm_sec=49, tm_wday=5, tm_yday=54, tm_isdst=-1) <class 'time.struct_time'>
print(time.mktime(timeArray))
# 1550906883.0

# 快所获得时间戳
print(time.time())
# 1550906883.9737751
print(int(time.time()))
# 1550906883


# 时间戳转换成时间
timestruck = 1550905859
st = time.localtime(timestruck)
print(st)
# time.struct_time(tm_year=2019, tm_mon=2, tm_mday=23, tm_hour=15, tm_min=10, tm_sec=59, tm_wday=5, tm_yday=54, tm_isdst=0)
print(time.strftime("%Y-%m-%d %H:%M:%S", st))

转载于:https://www.jianshu.com/p/aa20419c2b65