Skip to content

Use Datetime Now Not Today

Documentation Index

Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt

Use this file to discover all available pages before exploring further.

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

Section titled “Sourcery refactoring id: use-datetime-now-not-today”

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

from datetime import datetime
print(datetime.today())
from datetime import datetime
print(datetime.now())

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