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 trialBrendan 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,457 PointsHey Brendan Dzeguze, you are on the right path, Let's get finish this together
- The module
datetime
was imported correct. By naming theto_string
parameter "datetime", the imported module name is overwritten inside the function. This makes it not possible to use anydatetime
module functions. avoid using module names or builtin function names as parameter names. Try usingdtime
as parameter name. - To convert to a string, a
datetime
object has astrftime
method. 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 thestring
andfrmt
are passed into the function. Just usingdatetime
points 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!!