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 trialFarah Hassan
2,519 PointsI'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
Treehouse TeacherHi 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!
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
.
Farah Hassan
2,519 PointsFarah Hassan
2,519 PointsYes, thank you! That makes sense.
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherFarah Hassan! I originally posted something that was incorrect. The
datetime
library contains an object calleddatetime
which is a bit confusing, I know. Thetimedelta
is also an object in that library. Check my updated response