Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Dates and Times in Python Dates and Times Manipulating Time Already

gaetano amoroso
gaetano amoroso
2,993 Points

date time in pyttho firsth lesson

what is the differ between datetime.datetime and datetime only? When I call dir on both, I get different results why this? I thought that when I wrote datetime.datetime I'm working on instance directly in implicit way avoiding of writing instance = datetime() and hence datetime.datetime is same instance.datetime. I'm more confuse I have a big headache , can anyone help me?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,154 Points

Hi Gaetano! I remember this confusing me when I first met datetime. The difference is just a single datetime refers to the library module datetime and the other reference datetime.datetime refers to the datetime object within the module datetime. In practice, this becomes:

import datetime  # <-- import the module datetime

# to see the object datetime, you need reference the module namespace
datetime.datetime.now()  # <-- run the 'now' method on the 'datetime' object from the 'datetime' namespace. Yikes!

Alternatively, you can import the datetime object and refer to it directly:

from datetime import datetime  # <-- import the datetime object from the module datetime

# Now you can refer to the object datetime directly because it is now in the local namespace
datetime.now()  # <-- run the 'now' method on the 'datetime' object directly

If using the latter example and then try to refer to datetime.datetime you will get the error similar to:

AttributeError: type object 'datetime.datetime' has no attribute 'datetime'

gaetano amoroso
gaetano amoroso
2,993 Points

Simply wonderful response I now I understand how datetime works.

You helped me a lot thank you very much.