我的日期格式类似于 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 中实现这一点?提前致谢
最佳答案
您的日期字符串是 "%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
关于Python - 时间数据与格式不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51027836/