Answer to Question #231259 in Python for bhuvana

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".


1
Expert's answer
2021-09-03T15:18:28-0400
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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS