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 Staff

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
6,349 Points

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