Weekends
Given two dates D1 and D2, write a program to count number of Saturdays and Sundays from D1 to D2 (including D1 and D2).
The date in string format like "8 Feb 2021".
Input
The first line of input will contain date D1 in string format.
The second line of input will contain date D2 in string format.
Output
The output should a single line containing two integers separated by space.
Explanation
For example, if given dates are "25 Jan 2021" and "14 Feb 2021", the Saturdays and Sundays dates from "25 Jan 2021" to "14 Feb 2021" are
"30 Jan 2021" is a Saturday
"31 Jan 2021" is a Sunday
"6 Feb 2021" is a Saturday
"7 Feb 2021" is a Sunday
"13 Feb 2021" is a Saturday
"14 Feb 2021" is a Sunday
So output should be
Saturday: 3
Sunday: 3
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
import datetime
print("Input format for dates should be: 27 April 2021")
start_date = input("Enter start date: ")
end_date = input("Enter end date: ")
date_start1 = datetime.datetime.strptime(start_date, '%d %b %Y')
date_end1 = datetime.datetime.strptime(end_date, '%d %b %Y')
day = datetime.timedelta(days=1)
count_saturday = 0
count_sunday = 0
while date_start1 <= date_end1:
if date_start1.isoweekday() == 6:
count_saturday += 1
if date_start1.isoweekday() == 7:
count_sunday += 1
date_start1 += day
print("Saturday: ", count_saturday)
print("Sunday: ", count_sunday)
Comments
Leave a comment