Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string date into date format in python?

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 datetime.strftime and datetime.strptime. it is not accepting this format.

like image 687
Wasim Iqbhal Avatar asked Sep 02 '25 06:09

Wasim Iqbhal


1 Answers

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

like image 188
Mazdak Avatar answered Sep 04 '25 20:09

Mazdak