简体   繁体   English

将字符串日期转换为ISO8601格式

[英]Convert String date to format ISO8601

I have a date of format : 13 July 2017 22:00 that I would like to convert to an ISO date format of type 2017-07-13T22:00:00+0100 . 我有一个日期格式: 13 July 2017 22:00 ,我想转换为类型为2017-07-13T22:00:00+0100ISO日期格式。

So far I have managed to do this: 到目前为止,我已经做到了:

datetime_object = datetime.strptime(date_start, '%d %B %Y %H:%M')

where date_start = '13 July 2017 22:00' 其中date_start = '13 July 2017 22:00'

this returns 2017-07-13 22:00:00 ... close but not the same. 这将返回2017-07-13 22:00:00 ...关闭但不一样。

How can I convert it to the type I need ? 如何将其转换为所需的类型?

Use datetime.isoformat() : 使用datetime.isoformat()

>>> datetime_object = datetime.strptime('13 July 2017 22:00', '%d %B %Y %H:%M')
>>> datetime_object.isoformat()
'2017-07-13T22:00:00'

Note that this produces a timestamp in a "local time zone" (since your initial date has no time zone information attached). 请注意,这会在“本地时区”中产生一个时间戳(因为您的初始日期没有附加时区信息)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM