I have a python program that takes as input the week of the year in the format: YYYY-W#
The program uses this input to generate values for each day of the week and output data to an excel file.
I am getting the error: ValueError: day is out of range for month
and I realized I am getting this error when days of the week are in separate months (for example monday, tuesday are November, wednesday, thurday etc. are December).
The following code is how I get the values for days of the week:
monday = datetime.datetime.strptime(week + '-1', '%G-W%V-%u')
tuesday = datetime.datetime(monday.year, monday.month, monday.day - monday.weekday() + 1)
wednesday = datetime.datetime(monday.year, monday.month, monday.day - monday.weekday() + 2)
thursday = datetime.datetime(monday.year, monday.month, monday.day - monday.weekday() + 3)
friday = datetime.datetime(monday.year, monday.month, monday.day - monday.weekday() + 4)
saturday = datetime.datetime(monday.year, monday.month, monday.day - monday.weekday() + 5)
sunday = datetime.datetime(monday.year, monday.month, monday.day - monday.weekday() + 6)
This code works fine if the entire week is within the same month, is there a way to modify this to account for weeks that may not all be within the same month?