0

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?

NicLovin
  • 317
  • 3
  • 20

2 Answers2

2

Use timedelta:

tuesday = monday + datetime.timedelta(days=1)
wednesday = ... # (and so on)
Rustam A.
  • 809
  • 8
  • 15
  • Is there a way to do this independent of what the day is today? For example if I do ```monday = datetime.datetime.strptime('2021-W47'))``` Which is not the current week but the previous, then I do ```tuesday = monday + datetime.timedelta(days=1)``` I get the tuesday from this week instead of the tuesday from the previous week – NicLovin Nov 29 '21 at 18:22
  • Check your code. And maybe print it in other question if you won't find bug. My code gets Tuesdays from specified week all the time. And of course goes into next year on week 52. – Rustam A. Nov 29 '21 at 20:10
  • I ended up just using this: ```days_in_week = [datetime.datetime.strptime(week + str(x), "%G-W%V-%u") for x in range(-7,0)]``` to solve the problem, the timedelta does work as well though – NicLovin Nov 29 '21 at 21:02
2

Combining the answer in the (slight) duplicate question of Get date from week number and the answer from Rustam A.:

from datetime import datetime, timedelta

d = "2021-W48"
monday = datetime.strptime(d + "-1", "%Y-W%W-%w")
week = [monday + timedelta(days=x) for x in range(7)]

print(week)
# [datetime.datetime(2021, 11, 29, 0, 0), datetime.datetime(2021, 11, 30, 0, 0), datetime.datetime(2021, 12, 1, 0, 0), datetime.datetime(2021, 12, 2, 0, 0), datetime.datetime(2021, 12, 3, 0, 0), datetime.datetime(2021, 12, 4, 0, 0), datetime.datetime(2021, 12, 5, 0, 0)]

(edit: when using ISO-weeks, do use %G-W%V-%u as you did)

davidverweij
  • 328
  • 1
  • 15
  • My only issue is as I've commented on the other answer is that using timdelta is returning me the date from the current week but the monday could be from any week of the year – NicLovin Nov 29 '21 at 18:29
  • As Rustam A. mentions, double check first that the Monday is properly created. The code in my answer does give me the correct date. You're answer in the comment on Rustam A.'s seems to do the job (as you say); it's similar to the code in my answer although it parses the user input 7 times (instead of once - see my answer) and does string operation instead of timedeltas. It's a difference in design, but either way, happy you got it working! – davidverweij Nov 30 '21 at 16:41