'Removing the time from datetime output (Python)

Remove the time from this code.

Current Code:

from datetime import datetime, timedelta
    
date = datetime.today() - timedelta(days=1)

print("Today's date:", date)

Output:

Today's date: 2022-05-11 18:44:01.832558

Desired Output:

Today's date: 2022-05-11


Solution 1:[1]

Your date value is actually a datetime object. Call date() on it to convert it to just a date:

>>> import datetime
>>> print(datetime.datetime.today().date())
2022-05-12

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Samwise