I have this GUI with the button "get dates for last week".
I programmed this in 2024, using datetime objects and the isocalendar() method - and the button worked like a charm, returning the dates for Monday (first date of the week) and Sunday (last date of the week)
In 2025 my program returns the wrong dates.
Running this code:
from datetime import datetime
str_today_iso = "2025-07-25"
dt_today_1st = datetime.strptime(str_today_iso, "%Y-%m-%d")
str_today_in_week_format = str(dt_today_1st.isocalendar().year) + " " + str(dt_today_1st.isocalendar().week) + " " + str(dt_today_1st.isocalendar().weekday)
dt_today_2nd = datetime.strptime(str_today_in_week_format, "%Y %W %w")
str_today_final = dt_today_2nd.strftime("%Y-%m-%d")
str_monday_this_week = str(dt_today_1st.isocalendar().year) + " " + str(dt_today_1st.isocalendar().week) + " 1"
print(f"today in iso-format: {str_today_iso}")
print(f"today in week-format: {str_today_in_week_format}")
print(f"today after conversion: {str_today_final}")
Returns this output:
today in iso-format: 2025-07-25
today in week-format: 2025 30 5
today after conversion: 2025-08-01
Double-checking with kalenderwoche.de the 25. July 2025 is Week 30. With a Friday it's day 5. Yet converting "2025 30 5" returns Aug 1 2025.
Exchanging the year (still using the 25th July):
- 2023 -> the result looks fine.
- 2024 -> the result looks fine.
- 2025 -> false result
- 2026 -> false result
- 2027 -> error - as the weekday returns a 7, which is invalid for weekday!
- 2028 -> the result looks fine
According to kalenderwoche.de and isoweeks.com Jan 01 2025 is in Week 1.
Hence, the week 30 in 2025 starts on SUN July 20th 2025
According to isocalendar() Jan 01 2025 is in Week 0.
Hence, the week 30 in 2025 starts on SUN July 27th 2025
Shouldn't that be standardized?
Does anybody have the same problem?
Do I have a logical error?
Using Python 3.13.1.
%u,%Vand%G, not%w,%Wand%Y- docs.python.org/3/library/…. Switching todatetime.strptime(str_today_in_week_format, "%G %V %u")the conversion is correct (and note ISO 8601 weekday is one-based, not zero-based - this should be a clue that%wisn't right). Similar result for 2019 discussed here: bugs.python.org/issue35841