do not use datetime.today()

This page is not yet available in Spanish. We are working on its translation.
If you have any questions or feedback about our current translation project, feel free to reach out to us!

Metadata

ID: python-best-practices/no-datetime-today

Language: Python

Severity: Notice

Category: Best Practices

Description

Avoid using datetime.today() and use instead datetime.now(). The two calls are equivalent (as mentioned in the official documentation) and the use of now() is more explicit than today().

Using today() makes you think it only returns the year/month/day but it returns a full timestamp. For this reason, prefer using now().

Non-Compliant Code Examples

from datetime import datetime
print("foo")
bla = datetime.today()  # use datetime.now() instead

Compliant Code Examples

from datetime import datetime
print("foo")
bla = datetime.now()
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis