Python時(shí)間戳與日期的相互轉(zhuǎn)化
使用庫(kù)??time?
?
import time
print(time.time()) #1643195041.575819 返回當(dāng)前時(shí)間戳 float
# time.time() 時(shí)間戳表示的是從1970年1月1日00:00:00開始按秒計(jì)算的偏移量。
print(time.localtime()) # 本地時(shí)區(qū)的struct_time(北京時(shí)間)
# time.struct_time(tm_year=2022, tm_mon=1, tm_mday=26, tm_hour=23, tm_min=56, tm_sec=5, tm_wday=2, tm_yday=26, tm_isdst=0) 與time,time() 時(shí)間戳對(duì)應(yīng)
print(time.gmtime()) # UTC時(shí)區(qū)的struct_time(格林尼治時(shí)間)
# time.struct_time(tm_year=2022, tm_mon=1, tm_mday=26, tm_hour=15, tm_min=56, tm_sec=5, tm_wday=2, tm_yday=26, tm_isdst=0) 從1970.1.1 08:00:00 算起
print(time.localtime(time.time())) #返回一個(gè)日期結(jié)構(gòu)體time.struct_time(tm_year=2022, tm_mon=1, tm_mday=26, tm_hour=19, tm_min=5, tm_sec=23, tm_wday=2, tm_yday=26, tm_isdst=0)
1.相互轉(zhuǎn)化
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))
# 2022-01-27
print(time.strptime('2001-07-18','%Y-%m-%d'))
# time.struct_time(tm_year=2001, tm_mon=7, tm_mday=18, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=199, tm_isdst=-1)
本文摘自 :https://blog.51cto.com/u