Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Dante Vittorelli
Dante Vittorelli
2,301 Points

Getting an error stating that the correct answer is wrong.

For some reason I am getting the error " Bummer! to_string returned the wrong string. Returned '24 September 2012'."
I don't understand why because the challenge is asking for exactly that date, yet it's not passing. Would anyone know why this is happening?

import datetime

def to_string(x):
    x = datetime.datetime.now()
    x = x.replace(year = 2012, month = 9, day = 24)
    z = x.strftime('%d %B %Y')
    return z

[MOD: added ```python markdown formatting -cf]

Dante Vittorelli
Dante Vittorelli
2,301 Points

Sorry, I clicked the checkbox that says "attach code to post" but I guess it didn't attach it.

import datetime

def to_string(x):
    x = datetime.datetime.now()
    x = x.replace(year = 2012, month = 9, day = 24)
    z = x.strftime('%d %B %Y')
    return z

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,082 Points

The error is your are overwriting the argument x with your own value.

import datetime

def to_string(x):
    #x = datetime.datetime.now()
    #x = x.replace(year = 2012, month = 9, day = 24)
    z = x.strftime('%d %B %Y')
    return z

This should work for Task 1

Dante Vittorelli
Dante Vittorelli
2,301 Points

Oh wow... my answer makes no sense now that I get it haha. Thank you for your help.