Krish Kk
Krish Kk

Reputation: 21

Python Time conversion from dd month year to numeric year month day

The goal is to convert date = '09-Aug-2021' to '2021-08-09'

So far I got this

from dateutil import parser
date = '09-Aug-2021'
new_date = parser.parse(date)
final_date = str(new_date.year) + '-' + str(new_date.month) + '-' + str(new_date.day)

The result for the above code is '2021-8-9' but I want '2021-08-09' ( month and day to be two digits) Thank you, any help is greatly appreciated.

Upvotes: 0

Views: 86

Answers (1)

altermetax
altermetax

Reputation: 706

You should use a formatter function for that:

final_date = new_date.strftime("%Y-%m-%d")

Upvotes: 1

Related Questions