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

Bummer! module 'datetime' has no attribute 'strptime'???

I'm highly confuzzled as to why this doesn't work.

Edit: And apparently it didn't attach my code. One sec...

Edit2: My code:

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

def from_string(date, formatting):
  dt = datetime.strptime(date, formatting)
  return dt

From the docs:

>>> # Using datetime.strptime()
>>> dt = datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M")
>>> dt
datetime.datetime(2006, 11, 21, 16, 30)

1 Answer

You are extremely close on this one; you're missing a 'datetime'.

Also, you can just return the code, you don't need to assign a variable to return.

def from_string(date, formatting):
    return datetime.datetime.strptime(date, formatting)