Convert string date into date format in python?

后端 未结 3 658
日久生厌
日久生厌 2021-01-21 05:07

How to convert the below string date into date format in python.

input:
date=\'15-MARCH-2015\'

expected output:
2015-03-15

I tried to use

3条回答
  •  我在风中等你
    2021-01-21 05:42

    You can use datetime.strptime with a proper format :

    >>> datetime.strptime('15-MARCH-2015','%d-%B-%Y')
    datetime.datetime(2015, 3, 15, 0, 0)
    

    Read more about datetime.strptime and date formatting: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

提交回复
热议问题