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

Jen Farrant
Jen Farrant
6,905 Points

string time, task 2 - stuck

I've tried every version of the date set up, but it keeps telling me that I've got the format wrong. I don't know what format it should be.

Some help would be appreciated thank you Jen

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


def from_string(date, time):
    new_date = datetime.datetime.strptime('date', "%d %B %Y")
    new_time = datetime.datetime.sprptime('time', "%H:%M")
    return new_date + new_time

4 Answers

Erik Burmeister
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Erik Burmeister
Python Web Development Techdegree Graduate 17,108 Points

Mine only worked when I did it with 'datetime.datetime.strptime(date, formats)' like Omar G. suggested.

import datetime

def to_string(time):
    return time.strftime('%d %B %Y')

def from_string(date, formats):
    return datetime.datetime.strptime(date, formats)
Erik Gabor
Erik Gabor
6,427 Points

Maybe you don't have to repeat datetime.datetime and sprptime is not correct.

diogorferreira
diogorferreira
19,363 Points

I believe in the function from_string, they are passing in the date, and the format just like in the examples they show, from_string("09/24/12 18:30", "%m/%d/%y %H:%M") => datetime. I think that combining both arguments would just result in 2 concatenated strings. So you would only need to use the new_date line and pass in the date and format that they give you.

This is the code I tested:

import datetime
def to_string(time):
    return time.strftime('%d %B %Y')

def from_string(date, formats):
    return strptime(date, formats)

Hope that helps!

Nicholas Tan
Nicholas Tan
Courses Plus Student 8,562 Points

tested this code and what it return in the challenge is "Bummer: name 'strptime' is not defined"

diogorferreira
diogorferreira
19,363 Points

I've tried the code again and it works perfectly for me, it only gives me that error when you don't import datetime. Are you sure you put it the same code?

Nicholas Tan
Nicholas Tan
Courses Plus Student 8,562 Points

adeded in the datetime.strptime(date, formats) then it worked for me.

try using datetime.datetime.strp(date, formats)