简体   繁体   中英

Converting date/time string into datetime object - format does not match

I'm trying to convert the string:

'2018-10-16T11:41:39.29166592Z'

Into a datetime object using strptime. It seems the string format is wrong.

Running the line below:

datetime.datetime.strptime('2018-10-16T11:41:39.29166592Z', '%Y-%m-%dT%H:%M:%S.%fZ')

outputs:

ValueError: time data '2018-10-16T11:41:39.29166592Z' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'

Using the format: '%Y-%m-%dT%H:%M:%S.%9fZ' doesn't work neither.

Thanks

Try using the dateparser module.

Ex:

import dateparser
print(dateparser.parse('2018-10-16T11:41:39.29166592Z'))

Output:

2018-10-16 11:41:39.291665+00:00

MoreInfo


Or dateutil

Ex:

from dateutil.parser import parse
print(parse('2018-10-16T11:41:39.29166592Z'))

MoreInfo

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