将字符串转换为 python 中的日期时间

Converting string to datetime in python

我有一个简单的三行脚本,可将字符串转换为 Python 中的日期时间。

from datetime import datetime

mydate='Feb-22-1732'

print(datetime.strptime(mydate,'%b-%dd-%Y'))

但是当我 运行 这段代码时,我收到一条错误消息:

ValueError: time data 'Feb-22-1732' does not match format '%b-%dd-%Y'

你能帮我理解我做错了什么吗?

提前致谢。

您不需要重复 d 两次 - %d handles two-digit days by definition:

%d - Day of the month as a zero-padded decimal number.

print(datetime.strptime(mydate,'%b-%d-%Y'))