記錄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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM