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.
Jen Farrant6,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
## 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
Erik BurmeisterPython 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 Gabor6,427 Points
Maybe you don't have to repeat datetime.datetime and sprptime is not correct.
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!
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?