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

darcyprice
darcyprice
4,442 Points

I believe that I have solved the Challenge (Task 2), but I get a message saying "Task 1 is no longer passing".

I completed Task 1 with the included code and didn't adjust it in Task 2.

I am not sure why Task 1 would now not be passing, unless I am misunderstanding the question, and Task 2 requires us to edit def to_string?

What am I doing wrong? Any help would be much appreciated!

Thanks all ! :)

timestrings.py
# 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(dt):
    return dt.strftime('%d %B %Y')

def from_string(str_dt, str_strftime)
    """
    Accept a date as a string (str_dt) and an strftime-compatible format string (str_strftime)
    and return a datetime created from time

    >>> from_string('09 March 2018', '%d %B %Y')
    datetime.datetime(2018, 3, 9, 0, 0, 0, 0, 0)
    """
    return datetime.datetime.strftime(str_dt, str_strftime)

2 Answers

Steven Parker
Steven Parker
212,257 Points

Any syntax error will invalidate the entire program and cause the re-validations to fail.

In this case it is caused by a missing colon at the end of the "def" line of the new function.

The function also has "strftime" instead of "strptime".

darcyprice
darcyprice
4,442 Points

Ahhhh what a silly mistake,

Thank you Steven, much appreciative! :)

I review the difference between strftime and strptime now too