Given two dates D1 and D2, write a program to count the number of Saturday and sundays from D1 to D2 (including D1 and D2). The date in string format is like "8 Feb 2012"
For example, if the 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 the output should be
Saturday: 3
Sunday: 3
Sample Input 1
25 Jan 2021
14 Feb 2021
Sample Output 1
Saturday: 3
Sunday: 3
import datetime
print("Enter dates input format example: 8 Feb 2021")
date_start_str = input("Enter start date: ")
date_end_str = input("Enter end date: ")
# convert string to date format
date_start = datetime.datetime.strptime(date_start_str, '%d %b %Y')
date_end = datetime.datetime.strptime(date_end_str, '%d %b %Y')
# initialization of the initial number of weekends
day = datetime.timedelta(days=1)
count_saturday = 0
count_sunday = 0
# iteration over all dates in the range
while date_start <= date_end:
if date_start.isoweekday() == 6:
count_saturday += 1
if date_start.isoweekday() == 7:
count_sunday += 1
date_start += day
# output a single line containing two space-separated integers
print(count_saturday,count_sunday)
Comments
Leave a comment