Python Dates and Times in Python Dates and Times strftime & strptime

Ariadna Rodriguez
Ariadna Rodriguez
6,110 Points

I think my reasoning is close but my syntax might be off.

I'm confused if the datetime_object should be passed in as an argument or not.

timestrings.py
## Examples
# to_string(datetime_object) => "24 September 2012"
# from_string("09/24/12 18:30", "%m/%d/%y %H:%M") => datetime

import datetime

def to_string(datetime_object):
    new_dt = datetime.strftime(datetime_object, '%d, %B, %Y')
    return new_dt

4 Answers

Steven Parker
Steven Parker
204,010 Points

You're close, but you should call the method directly on the object and not pass the object as an argument. Then, the format should not have commas in it:

    new_dt = datetime_object.strftime('%d %B %Y')
Ariadna Rodriguez
Ariadna Rodriguez
6,110 Points

Yes! That makes sense. Also the function does work with date_object passed as the argument. Thanks for your help.

Steven Parker
Steven Parker
204,010 Points

What's the exact syntax that passed the challenge with the object as an argument?

Ariadna Rodriguez
Ariadna Rodriguez
6,110 Points

import datetime

def to_string(datetime_object): new_dt = datetime.strftime('%d, %B, %Y') return new_dt

Steven Parker
Steven Parker
204,010 Points

Most curious! I get "Bummer: Try again!" when I paste that into the challenge, but that's what I would expect since the argument isn't being used in the code body at all (plus it still has the commas).