Categories
Python Answers

How to calculate number of days between two given dates with Python?

Spread the love

Sometimes, we want to calculate number of days between two given dates with Python.

In this article, we’ll look at how to calculate number of days between two given dates with Python.

How to calculate number of days between two given dates with Python?

To calculate number of days between two given dates with Python, we can subtract the 2 dates and use the days property from the returned object.

For instance, we write

from datetime import date

d0 = date(2022, 8, 18)
d1 = date(2022, 9, 26)
delta = d1 - d0
print(delta.days)

to subtract the 2 dates d0 and d1.

And then we get the days difference from delta.days.

Conclusion

To calculate number of days between two given dates with Python, we can subtract the 2 dates and use the days property from the returned 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 *