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

Jen Farrant
Jen Farrant
6,893 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)