/** * Provides utility methods for working with [dates](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Date). * * @internal */ import type TimeInterval from "../time/TimeInterval.js"; import type { TimeUnit } from "./units.js"; import type { TimeZone } from "../time/types.js"; /** * Gregorian wall-clock parts used to convert between an instant and a time zone. * * @internal */ export interface ZonedDateTimeParts { /** @internal */ year: number; /** @internal */ month: number; /** @internal */ day: number; /** @internal */ hour: number; /** @internal */ minute: number; /** @internal */ second: number; /** @internal */ millisecond: number; /** @internal */ offsetMinutes: number; } /** * Returns the addition or subtraction of a quantity of temporal units. Computations are performed on local dates, not UTC. * * @param date - The input date. * @param offset - The number of temporal units to add or subtract. * @param unit - The input temporal unit. * @param timeZone - Processing time zone. * @returns A new date with the added or subtracted time. * @internal * @example * // Construct a date representing the 18th of September 2020. * const currentDate = new Date(2020, 8, 18); * * // Add six months to the date. * const futureDate = timeUtils.offsetDate(currentDate, 6, "months"); * * // Format the date using the "en-US" locale. * const formatter = new Intl.DateTimeFormat("en-US", { * year: "numeric", * month: "numeric", * day: "numeric" * }); * const currentDateString = formatter.format(currentDate); * const futureDateString = formatter.format(futureDate); * * // Print computed date to console. * console.log(`Six months from ${currentDateString} is ${futureDateString}`); * // output: "Six months from 9/15/2020 is 3/15/2020" */ export function offsetDate(date: Date, offset: number, unit: TimeUnit, timeZone?: TimeZone): Date; /** * Returns the Gregorian wall-clock parts and UTC offset for an instant in a time zone. * * Use this when a consumer needs to display an instant in an IANA time zone or obtain the zone's effective offset. * * @param date - The instant to convert. * @param timeZone - The time zone in which to express the instant. * @returns The wall-clock parts and offset, or `null` when the date or time zone is invalid or before the common era. * @internal */ export function getZonedDateTimeParts(date: Date, timeZone: TimeZone): ZonedDateTimeParts | null; /** * Formats Gregorian calendar date parts as a date-only ISO string. * * @param parts - The calendar date to format. * @returns The formatted date, or an empty string when the date is invalid. * @internal */ export function formatCalendarDate(parts: Pick): string; /** * Converts a local date and time (as a time zone offset) to a JavaScript Date. * * Skipped wall times move forward by the transition gap. Repeated wall times use the earlier instant. * * @param date - The calendar date to convert. * @param minutesSinceMidnight - The wall-clock time in minutes since midnight. * @param timeZone - The time zone in which to interpret the calendar date and time. * @returns The corresponding instant, or `null` when the date, time, or time zone is invalid. * @internal */ export function createZonedDate(date: { day: number; month: number; year: number; }, minutesSinceMidnight: number, timeZone: TimeZone): Date | null; /** * Truncates or "rounds down" dates to the specified time unit. Computations are performed on local dates, not UTC. * * @param date - The input date. * @param interval - The time interval used to round the parsed date down. * @param timeZone - Processing time zone. * @returns A new date rounded to the specified time unit. * @internal * @example * // Construct a date representing the 18th of September 2020. * const date = new Date(2020, 8, 18); * * // Compute the first day of the current decade. * const truncatedDate = timeUtils.truncateDate(date, "decades"); * * // Format the date using the "en-us" locale. * const formatter = new Intl.DateTimeFormat("en-us", { * year: "numeric", * month: "numeric", * day: "numeric" * }); * const dateString = formatter.format(truncatedDate); * * // Print computed date to console. * console.log(`The first day of the current decade is: ${dateString}`); * // output: "The first day of the current decade is: 1/1/2020" */ export function truncateDate(date: Date, interval: TimeInterval, timeZone?: TimeZone): Date;