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) 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: