Answer to Question #242839 in Python for Rishi

Question #242839
Give two dates D1 and D2 write a program to count the number of saturdays and sundays from D1 to D2 the date in string formate is like 8feb 2021
1
Expert's answer
2021-09-28T00:49:09-0400
from datetime import datetime, timedelta
input1 = datetime.strptime(input(),"%d%b %Y")
input2 = datetime.strptime(input(),"%d%b %Y")
dates = (input1 + timedelta(idx )
     for idx in range((input2 - input1).days+1))
  
res_sat = 0
res_sun = 0
# summing all weekdays
for day in dates:
  #print(day.weekday())
  if day.weekday() == 5:
    res_sat+=1
  elif day.weekday() == 6:
    res_sun+=1
# printing
print("Total saturday's in range : " + str(res_sat))
print("Total sundays's in range : " + str(res_sun))
## If we want to print the total count only uncomment below line and comment above two lines.
#print(res_sat+res_sun)

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