Returning strings instead of digits with datetime.strptime

923 Views Asked by At

With the following code:

current_date = datetime.strptime(row[0], "%Y-%m-%d")
dates.append(current_date)

I want to return the dates in the string format (i.e. "Jan 2014") but it's returning the dates in the original format as "2014-01-01".

1

There are 1 best solutions below

0
On

The default string representation of a date object is a string representing the date in ISO 8601 format, 'YYYY-MM-DD'. To represent dates in other format, use date.strftime(), e.g.:

>>> from datetime import date
>>> date.today().strftime('%b %d, %Y')
'Sep 19, 2017'

For a complete list of formatting directives, see strftime() and strptime() Behavior.