Python解析日期字符串到日期 [英] Python parse datestring to date
本文介绍了Python解析日期字符串到日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Python分析datetime字符串的日期.输入值的格式为:
I am trying to parse a datetime string to date in Python. The input value is of the form:
"February 19, 1989"
到目前为止,我一直在尝试
I have been trying so far
datetime.datetime.strptime("February 19, 1989", "%B %d, %y")
但是我总是出错.解析这样一个日期的正确方法是什么?谢谢!
but I am always getting error. What is the right way to parse such a date? Thank you!
推荐答案
以下工作原理(将小写的 y
更改为大写的 Y
):
The following works (changed small case y
to uppercase Y
):
datetime.datetime.strptime("February 19, 1989", "%B %d, %Y")
原因是%y
用于 99
, 98
等,而%Y
用于整整一年.您可以在此处上阅读完整文档.
The reason is that %y
is for 99
, 98
and such, while %Y
is for full year. You can read the full documentation on this here.
这篇关于Python解析日期字符串到日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文