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

Python Dates and Times in Python (2014) Dates and Times strftime & strptime

Datetime Vs Datetime.Datetime?

So I've gotten the answer down, and I believe I understand, to an extent anyway, why the answer is what it is.

What I'm still not comprehending though is why to use 'strftime', it only needs to be called attached to the argument in the function call, but to use 'strptime', we need to use datetime.datetime.strptime.

Usually I can piece together why somethings works but this one has me stumped right now. Any insight?

Many thanks in advance.

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_object):
    return datetime_object.strftime('%d %B %Y')

def from_string(string_form, strf_format):
    return datetime.datetime.strptime(string_form, strf_format)

1 Answer

If I'm correct, datetime is a module, whereas datetime.datetime is a class. I think that strftime is a method on this class. I am by no means certain about this. Dates in Python are horrendously confusing, especially if you are messing with time zones.

Yeah, I went back to review the courses looking for a solid answer on this. I've even heard Kenneth slip up and say Datetime, rather than datetime.datetime, and he made sure to correct himself on it - But that being the case, the difference between why one is called differently than the other is never explicitly laid out.

Thank you though, it definitely helps to know I'm not just completely lost at this point in the courses.