How to subtract a day from a date?

You can use the datetime module in Python to subtract a day from a date. Here's an example code snippet:

from datetime import datetime, timedelta

# Define a date
date = datetime(2022, 1, 1)

# Subtract one day from the date
subtracted_date = date - timedelta(days=1)

# Output: 2021-12-31 00:00:00

In this example, we first import the datetime and timedelta classes from the datetime module. We then define a date using the datetime class. To subtract a day from the date, we create a timedelta object with the number of days to subtract (in this case, 1) and subtract it from the date using the subtraction operator (-). The result is a new datetime object representing the date one day earlier than the original date.

You can also use the date.replace() method to subtract days from a date

from datetime import datetime

date = datetime(2022, 1, 1)
subtracted_date = date.replace(
# Output: 2022-01-31 00:00:00