Question #231259

Given two dates D1 and D2, write a program to count the number of Saturdays and Sundays from D1 to D2 (including D1 and D2).

The date in string format is like "8 Feb 2021".


Expert's answer

import datetime
print("Enter the dates in string format is like 8 Feb 2021")
firstDate = input("Enter the first date: ")
secondDate = input("Enter the second date: ")


startDate = datetime.datetime.strptime(firstDate, '%d %b %Y')
endDate = datetime.datetime.strptime(secondDate, '%d %b %Y')


day = datetime.timedelta(days=1)
numberOfSaturday = 0
numberOfSunday = 0


while startDate <= endDate:
    if startDate.isoweekday() == 6:
        numberOfSaturday += 1
    if startDate.isoweekday() == 7:
        numberOfSunday += 1
    startDate += day


print("Saturday:  "+ str(numberOfSaturday)+"\nSunday:  "+str(numberOfSunday))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS