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

Keith Griffin
Keith Griffin
4,833 Points

How do you enter the value of the datetime object

I've tried entering the date several different ways and always get a value error.

timestrings.py
import datetime

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

to_string("24/09/2012")

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Keith Griffin ! I can see that you've made some effort with this, but aren't quite there yet. The to_string should return a string. The parameter datetime_object (as you've named it) is a datetime object already. You can call the strftime directly on that object and pass in the string '%d %B %Y'. You do not need to call the function yourself. Treehouse tests will call the function and pass a datetime object.

Hope this helps! :sparkles:

Tim McMacken
Tim McMacken
9,831 Points

Thanks Jennifer, I was also calling strptime instead of strftime!