To format a date in Python, you can make use of the strftime()
method from the datetime
module. Here's how you can format the date "01-01-2019" as per your requirement:
main.py176 chars8 lines
In the code above, we first use strptime()
to convert the date string into a datetime
object, by specifying the current format of the date string ("%d-%m-%Y"). We then use strftime()
to format the date object into the desired format ("%Y-%m-%d"). Finally, we print the formatted date which would be "2019-01-01".
Hope this helps!
gistlibby LogSnag