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


to_string code challenge 1 of 2

I haven't seen anyone post a question to this challenge yet.

I was following a stackoverflow.com post to a similar question: http://stackoverflow.com/questions/2316987/converting-a-string-to-a-formatted-date-time-string-using-python

I tried converting a string from a datetime, but got an error stating "Bummer! Try again."

## Examples
# to_string(datetime_object) => "24 September 2012"
# from_string("09/24/12 18:30", "%m/%d/%y %H:%M") => datetime
import datetime
def to_string(datetime):
  dt = datetime.datetime.strftime('24112012', '%d/%B/%y')
  return dt


I didn't make a datetime object, but instead went straight from the function to the return output. I tried making an example before.

import datetime
def to_string(datetime_1):
  return datetime_1.strftime('%d %B %Y')

3 Answers

Hey there! I think the problem is that you're overwriting the datetime object that you're importing inside of the function definition. Instead of naming the passed in parameter 'datetime', try calling it something else (dt maybe?). That way, when you call datetime.datetime.strftime, python will know you're referring to the datetime package you imported. I believe this is true because when I run your function exactly, I get the error 'datetime.datetime' object has no attribute 'datetime.'

Hi Adam, I see what you mean about having the object as the same name. I updated my code above but am still getting an error. I kept datetime as a function parameter.

Well you're still gonna have the same problem, regardless, but there might be something else wrong. What is your error?

I got the same error as before stating, "Bummer! Try again."