Categories
Python Answers

How to subtract a day from a date with Python?

Spread the love

Sometimes, we want to subtract a day from a date with Python.

In this article, we’ll look at how to subtract a day from a date with Python.

How to subtract a day from a date with Python?

To subtract a day from a date with Python, we can use a timedelta object.

For instance, we write

from datetime import datetime, timedelta
    
d = datetime.today() - timedelta(days=days_to_subtract)

to subtract days_to_subtract days from today by creating a timedelta object with timedelta and then subtracting that from datetime.today().

Conclusion

To subtract a day from a date with Python, we can use a timedelta object.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *