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".
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))
Comments
Leave a comment