在python中将字符串日期转换为日期格式?

Was*_*hal 3 python datetime date

如何在python中将以下字符串日期转换为日期格式.

input:
date='15-MARCH-2015'

expected output:
2015-03-15
Run Code Online (Sandbox Code Playgroud)

我试着用datetime.strftimedatetime.strptime.它不接受这种格式.

Kas*_*mvd 8

您可以使用datetime.strptime适当的格式:

>>> datetime.strptime('15-MARCH-2015','%d-%B-%Y')
datetime.datetime(2015, 3, 15, 0, 0)
Run Code Online (Sandbox Code Playgroud)

阅读更多关于datetime.strptime日期格式的信息:https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior