I'm currently unsure on the logic to be used for the below problem and new to programming as well.(Currently learning python)
Trying to iterate thru every date for a given month - say 05/01 -- 05/31 and print it out in the below format.
Monday thru Friday dates are to be printed separately. Saturday & Sunday dates are to be printed separately.
If the month starts on say Friday - 05/01/2020, ouput should be like
as, its the last weekday of that week.
For the month of April 2020, output would be like below, as April month's 1st week started on Wednesday.
I managed to comeup with the below try, but not sure how to proceed further.
import sys
from datetime import date, datetime, timedelta
year = int(sys.argv[1])
month = int(sys.argv[2])
st_dt = int(sys.argv[3])
en_dt = int(sys.argv[4])
first_date = datetime(year, month, st_dt).date()
get_first_day = datetime(year, month, st_dt).isoweekday()
def daterange(startDate, endDate, delta=timedelta(days=1)):
currentDate = startDate
while currentDate <= endDate:
yield currentDate
currentDate += delta
for date in daterange(date(year, month, st_dt), date(year, month, en_dt), delta=timedelta(days=1)):
print(date)
date.py 2020 5 1 31 # script
Came up with a standalone 'if loop' and as i said before, not sure how to construct the bigger picture :(
if get_first_day == 1:
#print("Monday")
sec_d = first_date + timedelta(days=4)
elif get_first_day == 2:
sec_d = first_date + timedelta(days=3)
elif get_first_day == 3:
sec_d = first_date + timedelta(days=2)
elif get_first_day == 4:
sec_d = first_date + timedelta(days=2)
elif get_first_day == 5:
sec_d = first_date
#print("Friday")
else:
pass
print(f"Second date:{sec_d} ") -- which gave -- > Second date:2020-05-01