Python-时间数据与格式不匹配

时间:2018-06-25 16:14:34

标签: python

我有一个类似Apr 9 2018的日期格式,并想将其转换为2018-04-19

我的示例代码如下:

datetime.strptime(unformated_date, '%m %d %y').strftime('%y-%m-%d')其中unformated_date="Apr 9 2018"

但是我收到错误time data ' Apr 9 2018' does not match format '%m %d %y'

如何在python中实现呢?预先感谢

1 个答案:

答案 0 :(得分:2)

您的日期字符串为"%b %d %Y"

例如:

import datetime
unformated_date = "Apr 9 2018"    
print( datetime.datetime.strptime(unformated_date, "%b %d %Y").strftime('%y-%m-%d') )
print( datetime.datetime.strptime(unformated_date, "%b %d %Y").strftime('%Y-%m-%d') )

输出:

18-04-09
2018-04-09

MoreInfo

相关问题