Calculateur Semaines de Travail
Comptez les semaines de travail et les numéros de semaine ISO dans une plage de dates.
Loading...
Questions Fréquentes
Implémentation du Code
from datetime import date, timedelta
def get_iso_week(d: date) -> tuple[int, int]:
"""Return (iso_year, iso_week) for a date"""
iso = d.isocalendar()
return iso.year, iso.week
def get_monday_of_week(d: date) -> date:
"""Get the Monday of the week containing date d"""
days_since_monday = d.weekday() # Monday=0, Sunday=6
return d - timedelta(days=days_since_monday)
def count_work_weeks(start: date, end: date) -> list[dict]:
"""List all ISO work weeks in a date range"""
weeks = []
current = get_monday_of_week(start)
while current <= end:
iso_year, iso_week = get_iso_week(current)
friday = current + timedelta(days=4)
weeks.append({
'iso_year': iso_year,
'iso_week': iso_week,
'week_label': f'W{iso_week:02d}',
'start': current,
'end': friday,
'month': current.strftime('%B %Y'),
})
current += timedelta(weeks=1)
return weeks
# Usage
start = date(2026, 1, 1)
end = date(2026, 3, 31)
weeks = count_work_weeks(start, end)
print(f"Total work weeks: {len(weeks)}")
for w in weeks[:5]: # Show first 5
print(f" {w['week_label']} ({w['iso_year']}): {w['start']} - {w['end']}")
Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.