Skip to content

Use Datetime Now Not Today

Sourcery refactoring id: use-datetime-now-not-today

Description

Replace calls to datetime.datetime.today() with datetime.datetime.now(). They are functionally equivalent, but now is a more expressive name.

Before

from datetime import datetime

print(datetime.today())

After

from datetime import datetime

print(datetime.now())

Explanation

datetime.datetime.today() has a misleading name. It doesn't return a date object as the name today would imply, but a datetime object containing the current time. For this reason, the functionally equivalent datetime.datetime.now() is preferred. (See the Python Standard Library Docs .)

Back to top