欢迎光临
我们一直在努力

python时间戳转换,python时间戳转日期

代码

import pytzfrom datetime import datetimeimport time# 各时区实例utc = pytz.utcgeijing = pytz.timezone(“Asia/Shanghai”)pst = pytz.timezone(“US/Pacific”)tokyo = pytz.timezone(“Asia/Tokyo”)#时间戳loc_timestamp = time.time()print(“时间戳:%s” % loc_timestamp)# 转utc时间 datetime.datetime 类型utc_date = 便宜香港vps datetime.utcfromtimestamp(loc_timestamp)print(“UTC时间:%s” % utc_date)# 转utc当地 标识的时间utc_loc_time = utc.localize(utc_date)print(“UTC时间标记:%s” % utc_loc_time)fmt = ‘%Y-%m-%d %H:%M:%S %Z%z’# 转pst时间pst_time = utc_loc_time.astimezone(pst)print(“PST时间标记:%s” % pst_time)print(pst_time.strftime(fmt))# 转北京时间beijing_time = utc_loc_time.astimezone(geijing)print(“北京时间标记:%s” %beijing_time)print(beijing_time.strftime(fmt))

结果
时间戳:1594883228.5858855
UTC时间:2020-07-16 07:07:08.585886
UTC时间标记:2020-07-16 07:07:08.585886+00:00
PST时间标记:2020-07-16 00:07:08.585886-07:00
2020-07-16 00:07:08 PDT-0700
北京时间标记:2020-07-16 15:07:08.585886+08:00
2020-07-16 15:07:08 CST+0800

参考文章:
使用 pytz 处理 Python 中的时区问题
Python时间与日期操作(datetime、time、calendar)
python中时间、日期、时间戳的转换

98179072

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。