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

Zachary wathen
seal-mask
.a{fill-rule:evenodd;}techdegree
Zachary wathen
Python Development Techdegree Student 7,152 Points

the console is telling me that datetime does not have an attribute called strptime?

not sure what i am missing here...

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 (datetime1):
    new_date = datetime1.strftime("%d %B %Y")
    return new_date

def from_string (date_string, strftime1):
    date_object = datetime.strptime(date_string, strftime1)
    return date_object

1 Answer

The challenge says that module 'datetime' has no attribute 'strptime'. However, the module datetime has a class datetime which has a method strptime.

It should be sufficient to change your first line to from datetime import datetime, but the challenge does not accept that as a solution. Instead, you can change date_object = datetime.strptime(date_string, strftime1) to date_object = datetime.datetime.strptime(date_string, strftime1)