Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Dates and Times in Python Dates and Times strftime & strptime

Javi Caballero
Javi Caballero
6,864 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
218,596 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
6,864 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
6,864 Points

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

Steven Parker
Steven Parker
218,596 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
6,864 Points

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

Steven Parker
Steven Parker
218,596 Points

Javi Caballero — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!