Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 606
sreekanth Avatar asked Sep 11 '15 06:09

sreekanth


People also ask

What is the difference between datetime now and datetime today?

The DateTime. Now property returns the current date and time, for example 2011-07-01 10:09.45310 . The DateTime. Today property returns the current date with the time compnents set to zero, for example 2011-07-01 00:00.00000 .

What is datetime datetime now () in Python?

Here, we have used datetime.now() to get the current date and time. Then, we used strftime() to create a string representing date and time in another format.

What does datetime today return Python?

today() The today() method of date class under DateTime module returns a date object which contains the value of Today's date.

What is the difference between date and datetime in Python?

Python datetime Classesdatetime – Allows us to manipulate times and dates together (month, day, year, hour, second, microsecond). date – Allows us to manipulate dates independent of time (month, day, year).


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

like image 132
Tevin Joseph K O Avatar answered Sep 20 '22 10:09

Tevin Joseph K O