如何将此日期转换为日期时间?

时间:2019-09-04 11:00:49

标签: python datetime timestamp

我需要将这种日期转换为格式字符串2019-08-02T07:00:00 + 0000 到日期时间。

我尝试过这样:

date_1 = dt.datetime.strptime(date_1, "%Y-%m-%dT%H:%M+0000")

并这样:

date_1 = dt.datetime.strptime(date_1, "%Y-%m-%dT%H:%M%SZ")

但它不起作用。

2 个答案:

答案 0 :(得分:2)

您忘记了秒,您需要

dt.datetime.strptime(date_1, "%Y-%m-%dT%H:%M:%S+0000")

您可以通过依次从date_1中删除部分以及格式字符串来轻松调试此类内容:

dt.datetime.strptime("2019-08-02T07:00:00+0000", "%Y-%m-%dT%H:%M+0000")
#error
dt.datetime.strptime("2019-08-02T07:00:00", "%Y-%m-%dT%H:%M")
#unconverted data remains: :00
dt.datetime.strptime("T07:00:00", "T%H:%M")
#unconverted data remains: :00
dt.datetime.strptime("07:00:00", "%H:%M")
#now it should be obvious

答案 1 :(得分:-1)

import datetime
date_object = datetime.date.today()
print(date_object)