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
Lyric Abbott
35,595 Pointshelp
Create a new function named from_string that takes two strings, a date and a format, and returns a datetime based on them.
my code:
def to_string(date1): return date1.strftime("%d %B %Y")
def from_string(date_string, format): return datetime.strftime("%t %d %B %Y")
http://teamtreehouse.com/library/dates-and-times-in-python/dates-and-times/strftime-strptime
1 Answer
Samuel Webb
25,370 PointsYour goal for this is to use the strptime() method on the second one since you're trying to parse a string into a datetime. Basically your function from_string took the format parameter but it didn't use it anywhere. Here's what your code should look like.
import datetime
def to_string(dt):
return dt.strftime('%d %B %Y')
def from_string(dt, form):
return datetime.datetime.strptime(dt, form)
The reason I'm using date time twice there is because the first one is the module and the second one is the class. strptime() is s method called on the datetime class.
Also, although it will still pass, try not to use the keyword format as a variable in your functions.