pysnippets.dates.weekdays
Check for a weekday or weekend
is_weekday(day: date) -> boolReturnTruefor Monday to Friday.is_weekend(day: date) -> boolReturnTruefor Saturday and Sunday.
Tell weekdays from weekends using date.weekday().
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- 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))TrueEdit 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).
python 3.14 · in your browser
Loading Python 3.14 in your browser (≈7 MB, cached after the first time)…
Tab inserts four spaces. Press Escape, then Tab, to leave the editor. Control or Command plus Enter runs the examples.
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
- 3.11✓passes
- 3.12✓passes
- 3.13✓passes
- 3.14✓passes
Import
from 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.