Computer >> Computer tutorials >  >> Programming >> Python

Python program to find number of days between two given dates


To find the number of days between tow dates, we are using the Python datetime module. At first, import the required library −

from datetime import date

Create date objects and input the dates from which you want to calculate the days −

date1 = date(2021, 7, 20)
date2 = date(2021, 8, 30)

Get the difference between the above 2 dates in the form of days −

(date2 - date1).days

Example

Following is the code −

from datetime import date

# both the dates
date1 = date(2021, 7, 20)
date2 = date(2021, 8, 30)

print"Date 1 = ", date1;
print"Date 2 = ", date2;

resDays = (date2 - date1).days
print"Days between two dates = ", resDays

Output

This will produce the following output −

Date 1 = 2021-07-20
Date 2 = 2021-08-30
Days between two dates = 41