记录python 获取一小时前的时间戳

import time
import datetime


t=datetime.datetime.now()

#当前日期
t1 =t.strftime('%Y-%m-%d %H:%M:%S')
#转为秒级时间戳
ts1=time.mktime(time.strptime(t1, '%Y-%m-%d %H:%M:%S'))
#转为毫秒级
end_time=int(str(ts1*1000).split(".")[0])


#1小时前
t2=(t-datetime.timedelta(hours=1)).strftime("%Y-%m-%d %H:%M:%S")
#转为秒级时间戳
ts2=time.mktime(time.strptime(t2, '%Y-%m-%d %H:%M:%S'))
#转为毫秒级
start_time=int(str(ts2*1000).split(".")[0])

print("\n","*"*30)

print(start_time)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts2)))

print("*"*30)

print(end_time)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts1)))

print("*"*30,"\n")

转载自:https://www.cnblogs.com/sea-stream/p/12183018.html
posted @ 2020-12-15 11:35  没有头发  阅读(1070)  评论(0编辑  收藏  举报