I'm stuck with this python problem, I have a date in the format of month-day-year and I need to change it to year-month-day format
Asked
Active
Viewed 431 times
-1
-
Show me how the date looks like ? – ZdaR Jun 03 '15 at 05:53
-
Possible duplicate of http://stackoverflow.com/questions/10985312/parsing-date-string-in-python-convert-string-to-date – Freek Wiekmeijer Jun 03 '15 at 05:55
-
possible duplicate of [Parse date string and change format](http://stackoverflow.com/questions/2265357/parse-date-string-and-change-format) – Gurupad Hegde Jun 03 '15 at 06:01
1 Answers
1
>>> import datetime
>>> datetime.datetime.strptime("06/03/2015", "%m/%d/%Y").strftime("%Y-%m-%d")
'2015-06-03'
This takes a date (June 3, 2015 in this case) as a string and parses it to a datetime object using strptime
. It then converts that to a string in the format you requested (YYYY-mm-dd) using strftime

Andy
- 49,085
- 60
- 166
- 233