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

Jorge Grajeda
Jorge Grajeda
Python Development Techdegree Student 4,983 Points

timestrings.py task 1 of 2

Every time I change the argument it continues saying that either time or form are not being defined.

import datetime

def to_string(datetime):
    return time.strftime(form)

2 Answers

Steven Parker
Steven Parker
229,657 Points

Here's a few hints:

  • parameter and variable names should be different from package names (like "datetime") to avoid confusion
  • there is no "time" defined, the method must be called on the parameter
  • there is no "form" defined, you'll need to supply a literal string for the format
Asher Orr
.a{fill-rule:evenodd;}techdegree seal-36
Asher Orr
Python Development Techdegree Graduate 9,408 Points

Hi Jorge!

You should check out this section of the Python Docs (it's the section about strftime and strptime behavior.)

I would start by changing your argument to datetime_obj (as Steven said, parameter and variable names should be different from package names like "datetime" to avoid confusion.)

Now for your return statement:

import datetime

def to_string(datetime_obj):
    return time.strftime(form)

If you look at the Python docs, you'll see that strftime is a method called on date, datetime, and time objects to convert it to a string.

You are calling the strftime method on "time," but the checker doesn't know what that is. You should call it on datetime_obj instead.

One more thing: inside the parentheses after strftime, you need to specify the exact form in quotation marks.

The format codes are in the link at the top of this answer. It should look something like this:

.strftime("%m %d %B")
#these aren't the right codes for your problem. It's just an example.

I hope this helps. Let me know if you have further questions!