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

Javi Caballero
Javi Caballero
7,422 Points

Create a new function named from_string that takes two arguments: a date as a string and an strftime-compatible format s

Bummer: descriptor 'strftime' requires a 'datetime.date' object but received a 'str'

I'm not sure why it says that, could anyone tell me what I'm missing?

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_birthday): #Not Kenneth's birthday
    date_plan = datetime_birthday.strftime('%d %B %Y') 
    return date_plan

def from_string(date_object, strf_time):
    date_object = datetime.datetime.strftime(date_object, '%d %B %Y')
    strf_time = date_object.strftime('%d %B %Y')

    return strf_time

1 Answer

Steven Parker
Steven Parker
231,275 Points

Here's a few hints:

  • the first argument is named "date_object" — did you forget that both arguments are strings?
  • "strf_time" is the second argument, you'll need to use it but not assign anything to it
  • the "strftime" method returns a string, but this task needs to return a datetime
  • perhaps the second task needs to use a different datetime method
Javi Caballero
Javi Caballero
7,422 Points

So the edited version looks like this:

import datetime

def to_string(datetime_birthday): date_plan = datetime_birthday.strftime('%d %B %Y') return date_plan

def from_string(d1, strf_time): d1 = datetime.datetime.strptime(d1, '%m %d %y') return strf_time

But then I get this: Bummer: time data '15-9-24' does not match format '%m %d %y'

Is it something obvious? Probably, most likely, but I can't seem to see what it is.

Javi Caballero
Javi Caballero
7,422 Points

No matter what kind of date string I write, it won't accept it...

Steven Parker
Steven Parker
231,275 Points

When posting code, use Markdown formatting (as I have done here) to preserve the code's appearance.

But your 2nd function still does some rather curious things:

def from_string(d1, strf_time):
    d1 = datetime.datetime.strptime(d1, '%m %d %y')
    # this function produces a datetime, but it is being re-assigned to the first argument
    # also, it is being passed a fixed string instead of using the template that was given
    return strf_time
    # this returns the 2nd (string) argument that was passed in instead of the new datetime
Javi Caballero
Javi Caballero
7,422 Points

Ok, sorry for not using Markdown formatting. I got it though! Thanks for the tips!