Get the day of the week for a date
Print which weekday a given date falls on — for backup-window scripts ("only run on Sunday"), log rotation, conditional CI gates ("skip the weekly job on Friday"), or `cron` replacements that need day-of-week introspection.
How to get the day of the week for a date in each shell
date -d "2026-05-17" +%AOutput: `Sunday`. `+%A` is full name (`Monday`), `+%a` is short (`Mon`), `+%u` is ISO weekday number 1–7 with Monday=1 (Linux + macOS both ship this), `+%w` is 0–6 with **Sunday=0** (legacy POSIX). When scripting conditionals: use `+%u` — Monday=1 is intuitive AND `[ $(date +%u) -gt 5 ]` cleanly captures "is it weekend (Sat=6 or Sun=7)".
date -d "2026-05-17" +%Adate -d "2026-05-17" +%A(Get-Date "2026-05-17").DayOfWeek`.DayOfWeek` returns a `[System.DayOfWeek]` ENUM, NOT a string — values are `Sunday=0`, `Monday=1`, … `Saturday=6`. Comparing as string `if ($d -eq "Sunday")` works (enum stringifies on compare) but bitwise / range tests need integer cast: `if ([int](Get-Date).DayOfWeek -gt 0 -and [int](Get-Date).DayOfWeek -lt 6) { "weekday" }`. The enum is .NET-culture-neutral — output is always English, regardless of system locale.
powershell -NoProfile -Command "(Get-Date '2026-05-17').DayOfWeek"Pure cmd has no native day-of-week. The `%DATE%` env var includes the weekday on US locale (`Sun 05/17/2026`) but the format is locale-specific. Shelling to pwsh is the dependable path. `wmic path Win32_LocalTime get DayOfWeek /value` returns the integer (0=Sun) but is deprecated as of Windows 11 (wmic.exe is gone in 23H2+) — pwsh is the future-proof answer.
Equivalents listed for Bash, Zsh, Fish, PowerShell, cmd.exe.
Gotchas & notes
- **`%u` ISO Mon=1 vs `%w` POSIX Sun=0 — pick one and stick to it across a project**. Older shell scripts that grew organically tend to use `%w` (it was in POSIX from the start; `%u` came in later GNU/BSD versions). Modern scripts prefer `%u` because the weekend test is one comparison: `[ $(date +%u) -ge 6 ]` (Sat=6, Sun=7). With `%w`, the weekend test needs OR: `[ $(date +%w) -eq 0 ] || [ $(date +%w) -eq 6 ]`. Mixing both within one script is the bug source — `[ $(date +%u) -eq 0 ]` always evaluates to false (there is no day-zero in ISO), silently turning a "is Sunday?" check into a no-op.
- **`%A` and `%a` are locale-sensitive — DO NOT use in string comparisons**. `date +%A` on a French-locale Linux returns `dimanche`, not `Sunday`; on a Japanese server: `日曜日`. Scripts that test `[[ "$(date +%A)" == "Sunday" ]]` break silently when the script is run on a non-English locale. Either force the locale: `LC_TIME=C date +%A` (always English), or use the numeric format `%u`/`%w` which is locale-invariant. pwsh `.DayOfWeek` returns the enum which is always English — different behavior than bash. For user-facing output (logs, emails): pass through `%A` and let the locale do its job; for INTERNAL conditionals: numeric only.
- **"What day was 2000-01-01?" — Zeller's congruence as a calculator backup**: when no shell is handy (a calculator, a piece of paper) Zeller's formula gives day-of-week from year/month/day: `h = (q + floor(13(m+1)/5) + K + floor(K/4) + floor(J/4) - 2J) mod 7` where January and February are months 13 and 14 of the previous year. Result: `h=0` Saturday, `h=1` Sunday, … `h=6` Friday. Notable: 2000-01-01 was Saturday (Y2K turned out OK), 1969-07-20 (moon landing) was Sunday, 1989-11-09 (Berlin Wall fell) was Thursday. Worth knowing because it lets you VERIFY a script's output independently when a critical "only run on Sunday" trigger seems wrong.
- **Day-of-week from a `%s` epoch directly** — useful when you already have epoch in a variable: `date -d "@1747526400" +%u` (Linux, `@` prefix says "interpret as epoch seconds"); `date -r 1747526400 +%u` (macOS / BSD; same `-r` is also GNU but means "use this file's mtime as the date"). Cross-platform: `python3 -c "from datetime import datetime; print(datetime.fromtimestamp(1747526400).isoweekday())"` always works. pwsh: `([datetime]"1970-01-01Z").AddSeconds(1747526400).DayOfWeek` (`Z` for UTC, then convert to local with `.ToLocalTime()` if needed).
- **Computing "next Friday" / "previous Monday" with `date -d`**: GNU `date -d "next Friday" +%Y-%m-%d` is built-in (returns the upcoming Friday, or today if today is Friday — actually returns NEXT-Friday-meaning-7-days-from-now if today is Friday, which is a subtle gotcha — `date -d "Friday" +%Y-%m-%d` is "the most recent Friday or today" which is usually what you want). Other useful forms: `date -d "last Sunday"`, `date -d "next month"`, `date -d "Sunday + 2 weeks"`. macOS BSD `date` has NONE of this — fall back to Python `datetime + timedelta` or shell out to GNU date via `coreutils` brew package (`gdate -d "..."`).
Related commands
Related tasks
- Calculate days between two dates— Compute the integer number of days between two calendar dates — for SLA timers, log-window queries, license-expiry math, or backfill scripts that ask "how many days have passed since X?".
- Get the ISO week number of the year— Print which calendar week a date falls into — for sprint planning ("we are in W20"), retention dashboards (weekly bucketing), payroll periods, and any time-series workflow that wants "year-week" as a stable key.
- Parse a relative date like "next Friday" or "2 weeks ago"— Convert a human-readable relative-time expression ("3 days ago", "next Monday", "last quarter end") into an absolute date — for log filters, backfill ranges, scheduling, or any script that wants to accept user input the same way GitHub Actions cron does.