-2

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".

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
daquezada
  • 1,017
  • 2
  • 10
  • 15
  • Possible duplicate of [Convert datetime object to a String of date only in Python](https://stackoverflow.com/questions/10624937/convert-datetime-object-to-a-string-of-date-only-in-python) – Jeremy McGibbon Sep 19 '17 at 16:34

1 Answers1

2

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.

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378