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

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
11,910 Points

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