Write a Python program to calculate a number of days between two dates


This program is written in Python and it imports the "datetime" module and the "date" class from the "datetime" module. The line "date1 = date(2022,10,1)" creates a "date" object that represents the date "October 1st, 2022" . The "date" class takes three arguments: year, month, and day. The line "date2 = date(2023,1,1)" creates a "date" object that represents the date "January 1st, 2023".

The "date" class supports subtraction between two "date" objects, which calculates the difference between two dates in days. The line "date2-date1" calculates the difference between "date2" and "date1" in days. Finally, the line "print("Number of Days Between Two Dates :",(date2-date1))" prints the number of days between "date2" and "date1" . The output will be "Number of Days Between Two Dates : 92 days" .

Source Code

import datetime
from datetime import date
date1 = date(2022,10,1)
date2 = date(2023,1,1)
print("Number of Days Between Two Dates :",(date2-date1))

Output

Number of Days Between Two Dates : 92 days, 0:00:00

Example Programs