What is the difference between python functions `datetime.now()` and `datetime.today()`?

Question:

What is the difference between python functions datetime.now() and datetime.today() ?

In [1]: from datetime import datetime

In [2]: datetime.now()

Out[2]: datetime.datetime(2015, 9, 11, 12, 8,28, 909842)

In [3]: datetime.today()

Out[3]: datetime.datetime(2015, 9, 11, 12,8, 45, 175839)
Asked By: sreekanth

||

Answers:

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

Answered By: Tevin Joseph K O

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.

Answered By: Eric

See the documentation: now() provides an optional timezone, and can give more precision.

Answered By: Shep

Actually, there are three similar functions:

  • datetime.now(tz=None) Return the current local date and time.
  • datetime.today() Return the current local datetime, with tzinfo None.
  • datetime.utcnow() Return the current UTC date and time, with tzinfo None.

The first thing to note is from the doc:

This function (datetime.now()) is preferred over datetime.today() and datetime.utcnow()


I think there are two reasons:

First in functionality, datetime.now can accomplish what datetime.today and datetime.utcnow, but not vice versa.

>>> from datetime import datetime
>>> from datetime import timezone
>>>
>>> # datetime.now(tz=None) is equivalent to datetime.today();
>>> # datetime.utcnow() can't do this!
>>> datetime.now(tz=None)  # This is my local time
datetime.datetime(2022, 11, 2, 14, 59, 29, 265094)
>>> datetime.today()
datetime.datetime(2022, 11, 2, 14, 59, 29, 650280)
>>>
>>> # datetime.now(tz=timezone.utc) is equivalent to datetime.utcnow();
>>> # datetime.today() can't do this!
>>> datetime.now(tz=timezone.utc)
datetime.datetime(2022, 11, 2, 6, 59, 58, 298594, tzinfo=datetime.timezone.utc)
>>> datetime.utcnow()  # note that `tzinfo` is lost
datetime.datetime(2022, 11, 2, 6, 59, 58, 763605)

And what’s more, note that the tzinfo is lost in datetime.utcnow()‘s output.
Which means you have no chance to know the exact time out of the context.
This is also pointed out in the doc:

Warning: Because naive datetime objects are treated by many datetime methods
as local times, it is preferred to use aware datetimes to represent times in UTC.
As such, the recommended way to create an object representing the current time
in UTC is by calling datetime.now(timezone.utc).

Answered By: YaOzI
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.