Weekends
Given two dates D1 and D2,write a program to count the number of Saturday's and Sundays from D1 to D2(including D1 and D2). The date in string format is like "8 Feb 2021".
Input
The first line of input will contain date D1 in the string format.
The second line of input will contain date D2 in the string format.
Sample input 1
25 Jan 2021
14 Feb 2021
Sample output 1
Saturday: 3
Sunday: 3
Sample input 2
25 May 2019
22 Dec 2021
Sample output 2
Saturday:135
Sunday: 135
from datetime import datetime, timedelta
d1 = datetime.strptime(input(), '%d %b %Y')
d2 = datetime.strptime(input(), '%d %b %Y')
sat = 0
sun = 0
while d1 <= d2:
if d1.weekday() == 6:
sat += 1
elif d1.weekday() == 0:
sun += 1
d1 += timedelta(days=1)
print(f"Saturday: {sat}\nSunday: {sun}")
Comments
Leave a comment