我有一个纽约的弦乐格式的时间。我希望有时间在Python

问题描述 投票:0回答:1
输入(纽约时间字符串格式)='2024-11-01 13:00:00'

输出(以字符串格式的UTC时间)='2024-11-01 17:00:00'

python python-datetime
1个回答
0
投票
时间。它不会是“时区意识”,因此请应用本地时区,再次转换为UTC并再次格式:

import datetime as dt import zoneinfo as zi zone = zi.ZoneInfo('America/New_York') fmt = '%Y-%m-%d %H:%M:%S' s = '2024-11-01 13:00:00' print(s) # parse the time and apply the local time zone nyt = dt.datetime.strptime(s, fmt).replace(tzinfo=zone) # convert to UTC and format string utc = t.astimezone(dt.UTC).strftime(fmt) print(utc)
输出:

2024-11-01 13:00:00 2024-11-01 17:00:00
    
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.