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 trialJavi Caballero
7,422 PointsCreate 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?
## 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
231,248 PointsHere'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
7,422 PointsJavi Caballero
7,422 PointsSo 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
7,422 PointsJavi Caballero
7,422 PointsNo matter what kind of date string I write, it won't accept it...
Steven Parker
231,248 PointsSteven Parker
231,248 PointsWhen 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:
Javi Caballero
7,422 PointsJavi Caballero
7,422 PointsOk, sorry for not using Markdown formatting. I got it though! Thanks for the tips!