How to Calculate Number of Days Between Two Given Dates in Python
Created
Modified
Using timedelta
If you have two date objects, you can just subtract them, which computes a timedelta object.
In this program, we will calculate number of days between two given dates.
#!/usr/bin/python3
# Import module
import datetime
d1 = datetime.date(2022, 5, 1)
d2 = datetime.date(2022, 5, 22)
delta = d2 - d1
print(delta.days)
21