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 trialMUZ140057 Dumisani Nyamusa
8,029 Pointsstage 1 dates and times
Create a new function named from_string that takes two arguments: a date as a string and an strftime-compatible format string, and returns a datetime created from them.
## Examples
# to_string(datetime_object) => "24 September 2012"
# from_string("09/24/12 18:30", "%m/%d/%y %H:%M") => datetime
def to_string(date1):
return date1.strftime("%d %B %Y")
def from_string(date,strftime):
return datetime.str('arg1','arg2')
8 Answers
Jose Luis Lopez
19,179 Pointsdef to_string(datetime): return datetime.strftime("%d %B %Y")
def from_string(arg1, arg2): return datetime.datetime.strptime("10/21/15 00:00", "%m/%d/%y %H:%M")
it was hard but after reviewing the videos, I got it lol
tomasvukasovic
24,022 PointsHey the code corrected would be like this:
def from_string(arg1,arg2):
return datetime.datetime.strptime("{}".format(arg),"{}".format(arg2))
Kenneth Love
Treehouse Guest Teacherfrom_string
is supposed to produce a datetime
, not a string.
michaelangelo owildeberry
18,173 Points"Create a new function named from_string that takes two arguments: a date as a string and an strftime-compatible format string, and returns a datetime created from them."
Please help. code not passing:
import datetime def to_string(datetime_object): return datetime_object.strftime("%d %B %Y")
def from_string('arg1', 'arg2'): return datetime.datetime.combine("03/12/12 14:30", "%y/%m/%d %M:%H")
Kenneth Love
Treehouse Guest TeacherWhy are arg1 and arg2 quoted like strings?
michaelangelo owildeberry
18,173 PointsSo that strings can replace with strings. Argument is a string and formats too are strings.
Kenneth Love
Treehouse Guest TeacherOk, but args in function definitions are always variables, not whatever type you're looking for. They don't need, and will not work, with the quote marks.
michaelangelo owildeberry
18,173 PointsOh. I tried it this way.
def to_string(datetime_object): return datetime_object.strftime("%d %B %Y")
def from_string("03/12/12 14:30", "%y/%m/%d %M:%H"): return datetime.strptime(arg1, arg2)
Now, it says,"Task 1 is no longer passing." No clue as to why it would stop Task 1.
Kenneth Love
Treehouse Guest TeacherThe validator runs through every task every time, so breaking code you write in step 2 (as you did here), will cause step 1 to be invalid because it can no longer be validated.
You cannot define values as arguments when you create a function. You used values when you defined from_string
instead of argument names and then you tried to use these nonexistent argument names inside of the function.
michaelangelo owildeberry
18,173 PointsI have tried it this way and still no luck.
def from_string(arg1, arg2): return datetime.strptime("03/12/12 14:30", "%y/%m/%d %M:%H")
I am not getting the answer from my notes, what video should I turn to?
Kenneth Love
Treehouse Guest TeacherWhy aren't you using arg1
and arg2
in strptime
?
michaelangelo owildeberry
18,173 PointsI was trying to use that as the format, now I see the folly =)