Skip to content

getWeekNumber

getWeekNumber(dateStr: string, weekStartsOn?: "monday" | "sunday"): number
import { getWeekNumber } from "@northguild/gmt/plain/calculate";

Calculate the week number based on configurable week start day.

  • ISO (monday): Week 1 is the week containing the first Thursday (has at least 4 days in the year)
  • US (sunday): Week 1 is the week containing Jan 1 (first week has at least 1 day)
Parameter Type Description
dateStr string ISO PlainDate string (e.g. “2024-03-15”)
weekStartsOn "monday" | "sunday" optional: “monday” (default) or “sunday”

Week number (1-53), or null on invalid input

getWeekNumber("2024-01-01") // 1
getWeekNumber("2024-01-08") // 2
getWeekNumber("2024-12-31") // 1
getWeekNumber("2024-01-01", "sunday") // 1
getWeekNumber("invalid") // null

plain/calculate/getWeekNumber.ts