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

to_string

I get the error: Bummer! to_string returned the wrong string. Returned '24 September 2012'.

And that is an out put I was looking for...

Challenge: Create a function named to_string that takes a datetime and gives back a string in the format "24 September 2012".

timestrings.py
## 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_odject):
  datetime_object = datetime.datetime.strptime('09/24/12 18:30', '%m/%d/%y %H:%M')
  return datetime_object.strftime("%d %B %Y")

4 Answers

with return only:

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

it still won't let me pass the challenge.

To be honest, it's not really clear to me what has to be an out put in this challenge, is it this date, in this format: 24 September 2012, or can it be current date in the same format..? Do I have to make a from_string("09/24/12 18:30", "%m/%d/%y %H:%M") and then change the format to 24 September 2012..?

In this attempt your function is not accepting any arguments but it should take a datetime object.

Your original return statement was correct. You only needed to fix the typo on your argument name and remove the first line inside the function where you were creating a datetime using the example date given.

import datetime
def to_string(datetime_object):
  return datetime_object.strftime("%d %B %Y")

You don't want to assume any particular datetime here. A certain datetime is going to be passed into your function and then you have to return the properly formatted string for that particular datetime. "24 September 2012" was only given as an example so that you could see the format of the output and use that to come up with the format string "%d %B %Y"

Hi Christine,

A datetime object is going to be passed into your function so you don't need to create one inside your function. You only need your return statement. You also have a typo in your argument:

def to_string(datetime_odject):

You have a 'd' instead of a 'b'

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

The examples in the comments are just that, examples. They're not the exact solution or exact input.

import datetime

def to_string(dt): return dt.strftime("%d %B %Y")