Python日期时间:21-Feb-12至%d-%m-%Y

时间:2012-02-23 02:33:23

标签: python datetime

是否有一种简单的(内置)方式可以使用日期时间和/或日历将21-Feb-12转换为%d-%m-%Y?或者我需要解析字符串" 21-Feb-12"?任何Pythonic建议都欢迎。感谢。

1 个答案:

答案 0 :(得分:2)

使用strptime并为其指定正确的格式。

from datetime import datetime
print datetime.strptime("21-Feb-12", "%d-%b-%y")
#datetime.datetime(2012, 2, 21, 0, 0)

以下是strptime参考:http://docs.python.org/library/datetime.html#strftime-strptime-behavior