fluttersmith/py-snippetsGitHub ↗

Check for a weekday or weekend

Tell weekdays from weekends using date.weekday().

Dates#calendar#validationO(1) time✓ 5 examples pass on 3.11–3.14

Skip weekends when scheduling, or price weekend bookings differently.

weekdays.py✓ ruff · mypy --strict
from datetime import date


def is_weekday(day: date) -> bool:
    """Return ``True`` for Monday to Friday."""
    return day.weekday() < 5


def is_weekend(day: date) -> bool:
    """Return ``True`` for Saturday and Sunday."""
    return day.weekday() >= 5
  1. weekday(): Monday is 0, Sunday is 6
examples✓ 5 doctests · 3.11–3.14
passed on Python 3.11 to 3.14: >>> from datetime import datepassed on Python 3.11 to 3.14: >>> is_weekday(date(2024, 6, 14))Truepassed on Python 3.11 to 3.14: >>> is_weekday(date(2024, 6, 15))Falsepassed on Python 3.11 to 3.14: >>> from datetime import datepassed on Python 3.11 to 3.14: >>> is_weekend(date(2024, 6, 15))True
Edit the function or the examples and run the doctests here, on Python 3.14. Python loads only when you click (about 7 MB, cached after the first time).

How it works

  • weekday() returns 0 for Monday through 6 for Sunday.
  • Below 5 is a weekday, 5 and above is the weekend.
  • For a calendar where Sunday comes first, use isoweekday(), which runs 1 to 7.

Complexity: O(1) time and space

Importfrom pysnippets.dates.weekdays import is_weekday, is_weekendNo dependencies: copy weekdays.py into your project, or clone the repo.

Adapted from is-weekday and is-weekend in 30-seconds-of-python by the 30 seconds of code contributors, CC BY 4.0. Changed: Merged; the date is required instead of defaulting to import time; typed. Suggest an edit.