Python - Convert Date string to UTC timestamp

时间:2018-01-23 19:35:48

标签: python date utc

I have a string that says "Oct. 2, 2017, midnight" and want to convert it to a UTC timestamp.

Is there any good way to do this?

1 个答案:

答案 0 :(得分:1)

考虑到您的所有数据都在午夜,最简单的方法是删除, midnight字符串并将其转换。

import datetime
import pytz
s = "Oct. 2, 2017, midnight"
dt = datetime.datetime.strptime(s.split(', midnight')[0], '%b. %d, %Y')
dt_utc = dt.astimezone(pytz.utc)

如果还有其他时间,则您需要解释时间的文字并将其传递到strptime()