Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Dates and Times in Python (2014) Dates and Times strftime & strptime

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
231,269 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')

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

Steven Parker
Steven Parker
231,269 Points

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

import datetime

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

Steven Parker
Steven Parker
231,269 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).

It was this code it passed.