Skip to content

false result with datetime week and isocalendar() #137097

@4lexRed

Description

@4lexRed

Bug report

Bug description:

I have this function to deal with a gui button "get dates for last week".

I programmed the function in 2024, using datetime objects and the isocalendar() method.
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 (stripdown of the actual 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 its day 5. Converting "2025 30 5" yet returns the 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 as the highest number is 6 (0 to 6)
  • 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?
I mean it is called ISO-calendar.

Does anybody have the same problem?
Do I have a logical error?

Using Python 3.13.1 - This is such an obvious bug, that I doubt myself if I made an error.

CPython versions tested on:

3.13

Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy