How to Subtract a Day from a Date in Python

Created
Modified

Using timedelta Object

A timedelta object represents a duration, the difference between two dates or times.

See the following example:

#!/usr/bin/python3

# Import module
import datetime

t = datetime.datetime.today()

d = t - datetime.timedelta(days=1)
print(t)
print(d)
2022-05-08 21:06:31.549667
2022-05-07 21:06:31.549667

Syntax

class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)

All arguments are optional and default to 0. Arguments may be integers or floats, and may be positive or negative.

Related Tags

#subtract# #day# #date#