datetime.datetime.now() takes tzinfo as keyword argument but datetime.today() does not take any keyword arguments.
By default, now() executes with datetime.datetime.now(tz=None)
As quoted in the docs: https://docs.python.org/3.6/library/datetime.html#datetime.datetime.now
Answer from Tevin Joseph K O on Stack Overflowdatetime.now() return the current local date and time. If optional argument tz is None or not specified, this is like today(), but, if possible, supplies more precision than can be gotten from going through a time.time() timestamp (for example, this may be possible on platforms supplying the C gettimeofday() function).
datetime.datetime.now() takes tzinfo as keyword argument but datetime.today() does not take any keyword arguments.
By default, now() executes with datetime.datetime.now(tz=None)
As quoted in the docs: https://docs.python.org/3.6/library/datetime.html#datetime.datetime.now
datetime.now() return the current local date and time. If optional argument tz is None or not specified, this is like today(), but, if possible, supplies more precision than can be gotten from going through a time.time() timestamp (for example, this may be possible on platforms supplying the C gettimeofday() function).
See the docs for datetime.today and datetime.now, specifically this part from the latter link:
If optional argument tz is None or not specified, this is like today(), but, if possible, supplies more precision than can be gotten from going through a time.time() timestamp (for example, this may be possible on platforms supplying the C gettimeofday() function).
So in your example, both are the same, although specific platforms may provide more precision with datetime.now.
TIL that datetime.utcnow() is faster than datetime.now()
Difference between datetime.now and datetime.now() in the code ?
Not understanding difference between system.date.now() and datetime.datetime.now()
What is the difference between datetime.now() and datetime.today() in Python - Stack Overflow
I'm a student working on a Python project. In my code, I have a global variable:
today = date.today()
I am concerned with the date only, meaning I don't use time in my program. The only reason I would be concerned about time is for when it causes one today's date to switch to tomorrow's date.
Throughout my code, I reference this variable a bunch of times. For example:
-
today.day to get the current day of the month
-
today.strftime("%B") to get the current month name
-
today.year to get the current year, etc.
Recently, I have realized I need to deal with timezones. Upon looking up how to do this, I found some guides on how to use pytz. It seems fairly straightforward, however it all has to do with datetime.now() instead of date.today(), which I have been using throughout my code. I was hoping I could just swap in datetime.now(), but it messes things up because it has the date and the time, instead of just the date.
What is my best course of action? Is there a way for me to get date.today() to work with timezones/pytz, or do I need to re-work my code so that it can work with datetime.now()?
Hopefully this all makes sense. I just learned Python a few months ago. Thanks.
>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2021, 3, 5, 11, 30, 26, 254912)
>>> datetime.now()
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: module 'datetime' has no attribute 'now'
>>> type(datetime)
<class 'module'>
>>>type(datetime.datetime)
<class 'type'>
datetime is a python module in which "DateTime class" is present. When we call datetime.datetime, a new object is created of that class. "DateTime class" has different functions which provide the date, year, month, etc.
In brief, if you want to use multiple features of the datetime package, you will probably
import datetime
and need to refer to datetime.datetime.now() by its full name. If you only need features which are in datetime.datetime, you can abbreviate it by doing
from datetime import datetime
and then datetime.now() resolves to datetime.datetime.now() because the symbol datetime actually refers to datetime.datetime within your source file.
The nested structure with two identical labels referring to both the library and a specific submodule is not an ideal design. Many Python packages (both in the standard library, and more broadly) have unattractive import conventions where the only thing you could possibly want to import needs to be imported with the from package import submodule syntax, or you are stuck with having to use package.submodule whenever you want to refer to something in submodule.