python将时间戳转换为指定时区时间 |
您所在的位置:网站首页 › python日期转化 › python将时间戳转换为指定时区时间 |
直接上代码 根据时区代码(如:America/New_York)转换在这里我们使用pytz import pytz ts = 1566395745 tz = pytz.timezone('America/New_York') dt = pytz.datetime.datetime.fromtimestamp(ts, tz) dt.strftime('%Y-%m-%d %H:%M:%S')结果为:2019-08-21 09:55:45 指定时间差转换 from datetime import datetime, timedelta, timezone # 要转换的时间戳 ts = 1566474480 # 参数根据要转换的时区来确定,时区是UTC+2 时hours=2, UTC-3时hours=-3 td = timedelta(hours=2) tz = timezone(td) dt = datetime.fromtimestamp(ts, tz) dt = dt.strftime('%Y-%m-%d %H:%M:%S') print(dt)运行结果:2019-08-22 13:48:00 fromtimestamp的原型为(不知道该叫什么)datetime.fromtimestamp(timestamp, tz=None),当tz为None时,返回本地日期和时间。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |