@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.
31 lines (29 loc) • 1.33 kB
JavaScript
import { isOptionsArgument } from "../../internal/isObject.js";
import { startOrEndOfUtc } from "../../internal/startOrEndOfUtc.js";
// src/utc/calculate/startOfUtc.ts
/**
* Return the start of the specified date-time `unit` for a given UTC datetime string.
*
* - Converts to ZonedDateTime, sets to start of unit, converts back to Instant.
* - Supports: "year", "month", "week", "day", "hour", "minute", "second", "millisecond", "microsecond", "nanosecond", each also in its plural form (`"days"`), as Temporal accepts.
* - Returns "" for invalid input.
*
* @param value ISO UTC datetime string
* @param unit date or time unit, singular or plural, to specify the start
* @param options Where a week starts, and the precision of the result
* @returns UTC Instant string representing the start of the unit, or "" on invalid input
*
* @example startOfUtc("2024-03-15T14:30:45Z", "year") // "2024-01-01T00:00:00Z"
* @example startOfUtc("2024-03-15T14:30:45Z", "month") // "2024-03-01T00:00:00Z"
* @example startOfUtc("2024-03-15T14:30:45Z", "days") // "2024-03-15T00:00:00Z"
* @example startOfUtc("invalid", "year") // ""
*/
function startOfUtc(value, unit, options) {
try {
if (!isOptionsArgument(options)) return "";
return startOrEndOfUtc(value, unit, options ?? {}, false);
} catch {
return "";
}
}
export { startOfUtc };