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.