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

stage 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.

timestrings.py
## 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
Jose Luis Lopez
19,179 Points

def 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

Hey the code corrected would be like this:

def from_string(arg1,arg2):
  return datetime.datetime.strptime("{}".format(arg),"{}".format(arg2))
Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

from_string is supposed to produce a datetime, not a string.

"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
Kenneth Love
Treehouse Guest Teacher

Why are arg1 and arg2 quoted like strings?

So that strings can replace with strings. Argument is a string and formats too are strings.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Ok, 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.

Oh. 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
Kenneth Love
Treehouse Guest Teacher

The 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.

I 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
Kenneth Love
Treehouse Guest Teacher

Why aren't you using arg1 and arg2 in strptime?

I was trying to use that as the format, now I see the folly =)