@northguild/gmt
Version:
Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.
27 lines (25 loc) • 790 B
JavaScript
import { plainDateTimeOf } from "../../internal/inputChecks.js";
// src/plain/parse/parseDayOfWeekFromDateTime.ts
/**
* Return the day of week (1-7) for a given ISO 8601 datetime string.
*
* - Monday=1 through Sunday=7.
* - Returns null for invalid input.
*
* @param value ISO datetime string
* @returns Day of week (1-7) or null on invalid input
*
* @example parseDayOfWeekFromDateTime("2024-01-01T12:00:00") // 1 (Monday)
* @example parseDayOfWeekFromDateTime("2024-01-07T00:00:00") // 7 (Sunday)
* @example parseDayOfWeekFromDateTime("invalid") // null
*/
function parseDayOfWeekFromDateTime(value) {
const dateTime = plainDateTimeOf(value);
if (dateTime === null) return null;
try {
return dateTime.dayOfWeek;
} catch {
return null;
}
}
export { parseDayOfWeekFromDateTime };