0

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.

3
  • thinking about the issue, I filed a bug report on cpython's repository on github Commented Jul 25 at 9:15
  • 2
    If you want ISO 8601 weekday, week and year that's %u, %V and %G, not %w, %W and %Y - docs.python.org/3/library/…. Switching to datetime.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 %w isn't right). Similar result for 2019 discussed here: bugs.python.org/issue35841 Commented Jul 25 at 9:27
  • damn .... I always missed that in the docs. THX! Commented Jul 25 at 9:31

2 Answers 2

1

Thx to Jonrshape.
He commented on my question with the solution, that is documented in the python docs.

To use the ISO calendar values for the week and weekday, one has to use

  • %G - ISO 8601 year

  • %V - ISO 8601 week as a decimal number with Monday as the first day of the week.

  • %u - ISO 8601 weekday as a decimal number where 1 is Monday.

instead of "%Y %W %w" when converting the string to a datetime object with strptime()

Sign up to request clarification or add additional context in comments.

Comments

0

returning the dates for Monday (first date of the week) and Sunday (last date of the week)

This look like situation where calendar can be useful, as it has handy function for finding weekday, consider following simple example

import calendar
import datetime
dt = datetime.date(2025, 7, 25)  # fixed for presentation, use datetime.date.today() to get current date
wd = int(calendar.weekday(dt.day, dt.month, dt.day))  # 0 is Monday, 6 is Sunday
begin_dt = dt - datetime.timedelta(days=wd)
end_dt = dt + datetime.timedelta(days=6-wd)
print(begin_dt)  # 2025-07-21
print(end_dt)  # 2025-07-27

Like datetime, calendar is part of standard library.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.