Re-formatting a date in python

Viewed 35

So I have a date in this format "August 10, 2022". I need to reformat the date in this way "2022-08-10". How do I do that in python ?.

1 Answers

For this, you can use datetime, specifically on this behaviour.

from datetime import datetime 

a = "August 10, 2022"
b = datetime.strptime(a, '%B %d, %Y') # Converts to datetime format
c = b.strftime('%Y-%m-%d') # Converts from datetime to desired format
print(c)
# output
2022-08-10
Related