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 Where on Earth do Timezones Make Sense? Introduction to Timezones

I'm not clear on when I need to write datetime.what_I_want() vs datetime.datetime.what_I_want(). Is there a rule?

For example, datetime.timedelta as opposed to datetime.datetime.fromtimestamp, as seen in the previous challenge.

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Farah Hassan! It depends on how you imported datetime.

If you did this:

import datetime

... then you will need to use datetime.timedelta for example.

But if you did this:

from datetime import datetime, timedelta

... then you would only need timedelta.

The first one imports the entire datetime.py module which contains other things besides the datetime. So to access anything in it you need datetime.datetime because there are also things like datetime.date and datetime.timedelta. Hope this helps! :sparkles:

edited for accuracy

I originally posted an answer that was a bit incorrect. The timedelta is a part of the datetime library, not the datetime object in the datetime library. The datetime library contains an object that is also called datetime.

Yes, thank you! That makes sense.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Farah Hassan! I originally posted something that was incorrect. The datetime library contains an object called datetime which is a bit confusing, I know. The timedelta is also an object in that library. Check my updated response :smiley: