如何将字符串日期解析为正确的日期格式?

时间:2018-02-13 05:15:37

标签: python

我正在使用python" datefinder"图书馆获得"日期时间"宾语。但是,我遇到了一些问题,我有一个字符串:" 2012年6月的6天和#34;和" datefinder"库无法生成该字符串的正确日期。

我尝试过使用

datefinder.find_dates(date)
Result: 2012-06-13 00:00:00

上述结果肯定是错误的,我意识到罪魁祸首是" 6m"字。

我如何转换字符串" 2012年6月的第6天"正确的格式,如" 2012年6月6日"?

1 个答案:

答案 0 :(得分:0)

试试这个,它给你想要的o / p:

import datetime

p = datetime.datetime.strptime('the 6m day of June 2012','the %dm day of %B %Y')
print(p)

O / p就像:

2012-06-06 00:00:00

如果您想要所需格式06 June 2012,可以使用:

print(p.strftime("%d %B %Y"))