previousWeekday
Signature
Section titled “Signature”previousWeekday(value: string, dayOfWeek: number, options?: { inclusive?: boolean; }): stringimport { previousWeekday } from "@northguild/gmt/plain/calculate";Return a PlainDate ISO string for the previous occurrence of dayOfWeek on or before value.
dayOfWeekuses Temporal’s ISO numbering: 1 (Monday) through 7 (Sunday), consistent withgetDayOfWeek/parseDayOfWeekFromDate.options.inclusive(defaultfalse) controls what happens whenvaluealready falls ondayOfWeek:falsegoes back a full week (matching date-fns),truereturnsvalueas-is.- Returns “” on invalid input. Replaces date-fns’s sixteen
previous*functions with one parameterized call: | date-fns | gmt | | ––––––––––– | ——————————— | |previousMonday|previousWeekday(value, 1)| |previousTuesday|previousWeekday(value, 2)| |previousWednesday|previousWeekday(value, 3)| |previousThursday|previousWeekday(value, 4)| |previousFriday|previousWeekday(value, 5)| |previousSaturday|previousWeekday(value, 6)| |previousSunday|previousWeekday(value, 7)| |previousDay(v, n)|previousWeekday(value, n)|
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
value |
string |
ISO PlainDate string |
dayOfWeek |
number |
target ISO day of week (1-7, Monday-Sunday) |
Options
Section titled “Options”options
| Option | Type | Default |
|---|---|---|
inclusive? |
boolean |
— |
Returns
Section titled “Returns”ISO PlainDate string for the previous occurrence of dayOfWeek, or “” on invalid input
Examples
Section titled “Examples”previousWeekday("2024-03-15", 5) // "2024-03-08" (2024-03-15 is already a Friday, so it goes back a full week)previousWeekday("2024-03-15", 5, { inclusive: true }) // "2024-03-15"previousWeekday("2024-03-13", 5) // "2024-03-08" (Wednesday -> previous Friday)previousWeekday("invalid", 5) // ""previousWeekday("2024-03-15", 0) // "" (dayOfWeek out of range)