1

我有以下格式的日期时间和 UTC 偏移量。

22-01-2012 22:01:30 +0530

12-02-2012 13:00:34 -0400

如何使用 Python 中的 pytz 模块将其转换为 UTC?

4

2 回答 2

4

我会使用来自http://labix.org/python-dateutil#head-2f49784d6b27bae60cde1cff6a535663cf87497b的 dateutil.parser 。确保为您的 python 版本使用正确的版本。

import datetime
import dateutil.parser
import pytz
loc_dt = dateutil.parser.parse('22-01-2012 22:01:30 +0530')
loc_dt.astimezone(pytz.utc)
于 2012-04-19T22:43:10.080 回答
1

python3

>>> import time
>>> from datetime import datetime
>>> tm = '22-01-2012 22:01:30 +0530'
>>> fmt = '%d-%m-%Y %H:%M:%S %z'
>>> time.asctime(datetime.strptime(tm, fmt).utctimetuple())
'Sun Jan 22 16:31:30 2012'
于 2012-04-20T01:01:16.393 回答