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

Why are we using datetime.datetime.strptime()

I am not sure why we are using datetime.dateime.strptime()

... i.e. why we use datetime twice ??

I see plain old time. doesn't work like this, what is up there ??

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
import time

def to_string(my_datetime):
  return my_datetime.strftime('%d %B %Y')

def from_string(my_string_date, strftime_format):
  return datetime.datetime.strptime(my_string_date, strftime_format)

1 Answer

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello David.

I see you are fine with the code, so I presume you only want to know more about the datetime module.

First, here is a link to the python docs, you may want to have a look there: https://docs.python.org/2/library/datetime.html

As you may see in section 8.1.1 (just scroll down a little); you have the classes this module comes with. So datetime.date is for the date only, datetime.time is for the time and datetime.datetime is used for date and time.

Basically is the word is repeated twice but the first instance is for the module, the word after the . indicates the class of the module.

I hope I made it clear and with no mistakes!

;)

Vittorio

Ok, so it goes in the form <module>.<class>.<function/attribute>

I am not sure what a module is to be honest, but I see the python docs has a section on it so I'll read about it there

Thanks for the help :)

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hi David.

Just to try to be precise.

datetime.datetime is an object belonging to the datetime module.

I have tried to explain the reason behind the fact that we have 2 times the same word, hopefully my wording choices are not too bad.

;)

Vittorio