Answer to Question #201259 in Python for sajid pasha

Question #201259

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

sample input:

25 Jan 2021

14 Feb 2021

output:

Saturday: 3

Sunday: 3



1
Expert's answer
2021-05-31T23:56:54-0400
from datetime import datetime, timedelta

if __name__ == '__main__':
  print('Enter dates like: 31 May 2021')

  a = datetime.strptime(input('First date : '), '%d %b %Y')
  b = datetime.strptime(input('Last date : '), '%d %b %Y')

  day = timedelta(days=1)

  saturday = 0
  sunday = 0

  while a <= b:
    if a.isoweekday() == 6:
        saturday += 1
    if a.isoweekday() == 7:
        sunday += 1
    a += day

  print('satuday :', saturday)
  print('sunday :', sunday)

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