Numéro de semaine ISO
Trouvez le numéro de semaine ISO 8601 pour n'importe quelle date.
Questions Fréquentes
Implémentation du Code
from datetime import date
def iso_week_number(d: date) -> tuple[int, int, int]:
"""Return (ISO year, ISO week number, ISO weekday) for a given date.
ISO week 1 is the week containing the year's first Thursday.
Monday = 1, Sunday = 7
"""
iso_cal = d.isocalendar()
return iso_cal.year, iso_cal.week, iso_cal.weekday
def dates_in_iso_week(year: int, week: int) -> list[date]:
"""Return all 7 dates in a given ISO week."""
# ISO week 1, Monday
jan4 = date(year, 1, 4) # Jan 4 is always in week 1
start_of_week1 = jan4 - timedelta(days=jan4.isocalendar().weekday - 1)
from datetime import timedelta
start = start_of_week1 + timedelta(weeks=week - 1)
return [start + timedelta(days=i) for i in range(7)]
# Example
from datetime import timedelta
today = date.today()
y, w, wd = iso_week_number(today)
print(f"Today {today}: ISO week {w} of {y}, weekday {wd} (1=Mon)")
# All years have 52 or 53 ISO weeks
for yr in [2020, 2021, 2022, 2023, 2024]:
last_week = date(yr, 12, 28).isocalendar().week # Dec 28 is always in last week
print(f" {yr}: {last_week} ISO weeks")Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.