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
Brendan Dzeguze
Front End Web Development Techdegree Graduate 14,567 PointsCode not working. need help.
can someone help? my code isnt running.
import datetime
## Examples
# to_string(datetime_object) => "24 September 2012"
# from_string("09/24/12 18:30", "%m/%d/%y %H:%M") => datetime
def to_string(datetime):
return(datetime('%D %B %Y'))
def from_string(datetime):
return(datetime("%D/%B/%Y %H:%M"))
1 Answer
Chris Freeman
Treehouse Moderator 68,468 PointsHey Brendan Dzeguze, you are on the right path, Let's get finish this together
- The module
datetimewas imported correct. By naming theto_stringparameter "datetime", the imported module name is overwritten inside the function. This makes it not possible to use anydatetimemodule functions.avoid using module names or builtin function names as parameter names. Try using
dtimeas parameter name. - To convert to a string, a
datetimeobject has astrftimemethod.use `dtime.strftime() with your format string as the argument to convert a string
- The "%D" format returns a full date, you probably meant to use "%d" for the day value.
"'%d %B %Y"
- for
from_string, you will need to call the constructor methoddatetime.datetime.strptime(string, format)where thestringandfrmtare passed into the function. Just usingdatetimepoints to the module, to get the class and method. It can be confusing when the main object class is the same name as its module name.
More on strptime in the docs
Post back if you need more help! Good luck!!