How to subtract a day from a date?


Question

I have a Python datetime.datetime object. What is the best way to subtract one day?

1
657
3/1/2018 8:54:45 PM

Accepted Answer

You can use a timedelta object:

from datetime import datetime, timedelta

d = datetime.today() - timedelta(days=days_to_subtract)
1095
8/26/2016 3:07:44 PM

Subtract datetime.timedelta(days=1)


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon