What is the difference between python functions `datetime.now()` and `datetime.today()`?
PythonDatetimePython Problem Overview
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)
Thanks in advance.
Python Solutions
Solution 1 - Python
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).
Solution 2 - Python
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
.
Solution 3 - Python
See the documentation: now()
provides an optional timezone, and can give more precision.