简体   繁体   English

将日期/时间字符串转换为日期时间对象-格式不匹配

[英]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. 使用strptime转换为datetime对象。 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' ValueError:时间数据'2018-10-16T11:41:39.29166592Z'与格式'%Y-%m-%dT%H:%M:%S.%fZ'不匹配

Using the format: '%Y-%m-%dT%H:%M:%S.%9fZ' doesn't work neither. 使用格式: '%Y-%m-%dT%H:%M:%S.%9fZ'都不起作用。

Thanks 谢谢

Try using the dateparser module. 尝试使用dateparser模块。

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 dateutil

Ex: 例如:

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

MoreInfo 更多信息

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

相关问题 从字符串转换为日期时间时如何解决ValueError时间日期与格式不匹配的问题 - How to fix ValueError time date does not match format when converting from string to datetime Pandas 将字符串中的日期时间转换为日期时间格式 - Pandas converting date time in string to datetime format 在Python中将可读的日期和时间字符串转换为datetime对象 - Converting a readable date & time string to a datetime object in Python 转换为 pandas 日期时间数据与格式不匹配 - Converting to pandas datetime data does not match format 从周数转换为日期时间不起作用:“时间数据 '201T' 与格式 '%Y' 不匹配(匹配)” - Converting from week number to datetime not working: “time data '201T' does not match format '%Y' (match)” 当字符串与特定日期格式不匹配时将字符串转换为日期时间 - Converting string to a datetime when the string doesn't match a specific date format 将未知格式的日期字符串转换为datetime - Converting date string in unknown format to datetime 将日期列(字符串)转换为日期时间并匹配格式 - Convert date column (string) to datetime and match the format 从字符串解析日期时间“不匹配格式”错误 - “does not match format” error parsing date time from string Python 日期时间与格式字符串不匹配 - Python Datetime does not match format string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM