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

Chang Hyeon Lee
Chang Hyeon Lee
2,008 Points

I need help

What did I wrong?

import datetime

def to_string(dtime):
    dtime = datetime.datetime.strptime('21 October 2015', '%d %B %Y')
    return dtime.strftime('%d, %B, %Y')

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

2 Answers

Stuart Wright
Stuart Wright
41,118 Points

I believe there are two issues with your solution:

1 - You should delete the 'dtime =' line of your function. This line currently has the effect of changing the value of the variable dtime, meaning that no matter what datetime object you pass in, it will be changed to 21st October 2015, which isn't very useful. The final line alone is all you need...

2 - ... except there is one small error in this line, the commas inside the brackets are not needed, as the requested date format does not ask for commas between the day, month and year parts of the string.

Taylor Schimek
Taylor Schimek
19,318 Points

You're changing the value of dtime with the dtime = line. Everything else looks correct.

Actually, as Stuart Wright said, since code challenges are picky, you also should remove the commas in the string "%d, %B, %Y".