2

I'm a beginner in Python and I want to make a simple project to say how much time is left until Christmas. First I had a problem with the current time:

import datetime
now = datetime.now()
print(now)`

and the error said module 'datetime' has no attribute 'now'

Than I fixed it by doing from datetime import datetime but than the calculation for the date created an error The code:

from datetime import datetime
xmas = datetime.date(2020, 12, 25) - datetime.date.today()
now = datetime.now()
print(now)

And the error was: Exception has occurred: TypeError descriptor 'date' requires a 'datetime.datetime' object but received a 'int'
Please help me.
Have a nice day!

0

2 Answers 2

2

The datetime class is a little confusing to import because it's in a module that's also called datetime. When you do from datetime import datetime you're importing the class datetime into the current scope and can just do datetime.now(). If you just do import datetime you're importing the module datetime and so the class is datetime.datetime, making the now method datetime.datetime.now(). Clear as mud? :)

Further adding to the confusion, the datetime module has a date class, and the datetime class has a date method. You're mixing the two up in your code, calling the date method of the datetime class when you mean to be constructing a date object. Since you did from datetime import datetime, you did not import the datetime.date class at all.

Anyway, here's how to fix it:

import datetime

xmas = datetime.date(2020, 12, 25) - datetime.date.today()
now = datetime.datetime.now()
print(now)

Note that in this code, xmas is a datetime.timedelta object, and now is a datetime.datetime object.

You could also do:

from datetime import date, datetime

xmas = date(2020, 12, 25) - date.today()
now = datetime.now()
print(now)

Hopefully seeing the two examples side by side helps clarify the module vs class thing. It's all about different levels of indirection -- in the first example the import statement brings in the whole module, and your code needs to specify which classes to pull out of it. In the second example, the from ... import statement pulls those two classes out of the module as part of the import, and so the code can access them by those names directly. Since the names are all so similar, you can't rely on the name to know what kind of thing you're accessing unless you really understand what exactly the import statement did.

Sign up to request clarification or add additional context in comments.

2 Comments

can I ask for one more thing? Its how to calculate the time until chrismas like I did for the date. Because I'm having problems with it
time_till_xmas = datetime(2020, 12, 25) - datetime.now()
0

You have to import it like this:

from datetime import datetime

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.