python3 UTC时间和ISO时间转换

时间戳转日期and日期转时间戳

ticks = time.time() #获取时间戳
print("ticks:",ticks)
localdate = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ticks))#时间戳转日期
print("localdate:",localdate)
date_to_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(a))#日期转时间戳
print("date_to_time:",date_to_time)

----------------------------------
ticks: 1653623433.5224512
localdate: 2022-05-27 11:50:33
date_to_time: 2022-05-25 11:50:33

时间戳转ISO时间

ticks = time.time() #获取时间戳
localdate = datetime.fromtimestamp(ticks) #将时间戳转为datetime.datetime类型 2022-05-27 13:40:57.899528
#或者直接 datetime.now() 获取 datetime.datetime类型日期
#astimezone修改时区,isoformat()以iso格式输出时间字符串
iso_date = localdate.astimezone().isoformat()
print("ISO时间",iso_date)

去除iso后面的多余字符

location = iso_date.rfind('.')
date = iso_date[:location]
print("去除后:",date)
---------------------
ISO时间 2022-05-27T13:50:34.410212+08:00 
去除后:2022-05-27T13:50:34

你可能感兴趣的:(其他,utc,iso)