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")

输出

******************************
1578814030000
2020-01-12 15:27:10
******************************
1578817630000
2020-01-12 16:27:10
******************************

相关推荐