简体   繁体   中英

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 .

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'

this returns 2017-07-13 22:00:00 ... close but not the same.

How can I convert it to the type I need ?

Use 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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