from datetime import datetime, timedelta
import pytz
# way 1
t = datetime.utcnow() # t: datetime.datetime(2020, 5, 20, 13, 34, 21, 730673)
t_new = t + timedelta(hours=8) # utc转东八区
# way 2
tz = pytz.timezone('Asia/Shanghai')
t_new = t.astimezone(tz)
t_new = t_new.strftime('%Y-%m-%d %H:%M:%S') # t2: '2020-05-20 21:34:21'
python--datetime转时区
最新推荐文章于 2024-08-06 17:18:44 发布