@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.
33 lines (31 loc) • 1.22 kB
JavaScript
import { plainDateTimeOf } from "../../internal/inputChecks.js";
import { monthDayString } from "../../internal/monthDay.js";
// src/plain/parse/parseMonthDayFromDateTime.ts
/**
* Return the month-day (`--MM-DD`) of an ISO 8601 date-time string.
*
* - The year and the time are dropped, so the time never moves the day.
* - A `Z`, an offset or any annotation gives `""`, as `isValidDateTime` reads a plain
* date-time.
* - Only the ISO calendar is read.
* - Returns "" for invalid input.
*
* @param value ISO date-time string
* @returns The canonical `--MM-DD` string, or "" on invalid input
*
* @example parseMonthDayFromDateTime("2024-12-25T10:00:00") // "--12-25"
* @example parseMonthDayFromDateTime("2024-02-29T23:59:59.999999999") // "--02-29"
* @example parseMonthDayFromDateTime("2024-12-25") // ""
* @example parseMonthDayFromDateTime("2024-12-25T10:00:00Z") // ""
* @example parseMonthDayFromDateTime("2024-12-25T10:00:00[Europe/Paris]") // ""
*/
function parseMonthDayFromDateTime(value) {
const dateTime = plainDateTimeOf(value);
if (dateTime === null) return "";
try {
return monthDayString(dateTime.toPlainDate().toPlainMonthDay());
} catch {
return "";
}
}
export { parseMonthDayFromDateTime };