UNPKG

calendar-date

Version:

Immutable object to represent a calendar date with zero dependencies

199 lines (198 loc) 6.95 kB
export declare enum DayOfTheWeek { MONDAY = 1, TUESDAY = 2, WEDNESDAY = 3, THURSDAY = 4, FRIDAY = 5, SATURDAY = 6, SUNDAY = 7 } export declare class CalendarDate { readonly year: number; /** * Month of the year starting from 1 */ readonly month: number; /** * Day of the month starting from 1 */ readonly day: number; /** * Seconds passed since the unix epoch. Always calculated with a time and timezone set to T00:00:00.00Z. */ readonly unixTimestampInSeconds: number; /** * Day of the week starting from monday according to ISO 8601. Values from 1-7. */ readonly weekday: number; /** * cache for Intl.DateTimeFormat instances * @private */ private static dateTimeFormatterByTimezone; /** * Customizes the default string description for instances of `CalendarDate`. */ get [Symbol.toStringTag](): string; /** * Throws an Error for invalid inputs. * * @param isoString Format: yyyy-MM-dd */ constructor(isoString: string); /** * Throws an Error for invalid inputs. * * @param year Integer between 1-9999, other inputs may lead to unstable behaviour * @param month Integer between 1-12 * @param day Integer between 1-31 */ constructor(year: number, month: number, day: number); static isLeapYear(year: number): boolean; static getMaxDayOfMonth(year: number, month: number): number; private static getIntlDateTimeFormatter; /** * returns a CalendarDate instance for the supplied Date, using UTC values */ static fromDateUTC(date: Date): CalendarDate; /** * returns a CalendarDate instance for the supplied Date, using local time zone values */ static fromDateLocal(date: Date): CalendarDate; /** * returns a CalendarDate instance for the supplied Date, using the supplied time zone string */ static fromDateWithTimeZone(date: Date, timeZone: string): CalendarDate; /** * returns a CalendarDate instance for the current UTC Date */ static nowUTC(): CalendarDate; /** * returns a CalendarDate instance for the current Date using the local time zone of your environment */ static nowLocal(): CalendarDate; /** * returns a CalendarDate instance for the current Date using the supplied time zone string */ static nowTimeZone(timeZone: string): CalendarDate; /** * * @param isoString pattern YYYY-MM-DD */ static parse(isoString: string): CalendarDate; static max(...values: CalendarDate[]): CalendarDate; static min(...values: CalendarDate[]): CalendarDate; /** * Returns a copy of the supplied array of CalendarDates sorted ascending */ static sortAscending(calendarDates: CalendarDate[]): CalendarDate[]; /** * Returns a copy of the supplied array of CalendarDates sorted descending */ static sortDescending(calendarDates: CalendarDate[]): CalendarDate[]; /** * Returns the ISO string representation yyyy-MM-dd */ toString(): string; /** * Returns a string representation formatted according to the specified format string. * Supports the following Tokens: * * - **yyyy**: four digit year * - **yy**: two digit year * - **y**: year without padding * - **MM**: month padded to 2 digits * - **M**: month without padding * - **dd**: day padded to 2 digits * - **d**: day without padding * */ toFormat(pattern: string): string; /** * Returns a string representation formatted with the Intl.DateTimeFormat API. */ toFormat(locales: string, options: Pick<Intl.DateTimeFormatOptions, 'year' | 'month' | 'day' | 'weekday'>): string; /** * Used by JSON stringify method. */ toJSON(): string; toDateUTC(): Date; /** * @deprecated use toDateUTC instead. This method will be removed in the next major version. */ toDate(): Date; toDateLocal(): Date; /** * Returns the unix timestamp in seconds. */ valueOf(): number; equals(calendarDate: CalendarDate): boolean; /** * Checks if the month and year of this CalendarDate are the same as the month and year of the supplied CalendarDate. */ isSameMonth(calendarDate: CalendarDate): boolean; /** * Checks if the year of this CalendarDate is the same as the year of the supplied CalendarDate. */ isSameYear(calendarDate: CalendarDate): boolean; isBefore(calendarDate: CalendarDate): boolean; isAfter(calendarDate: CalendarDate): boolean; isBeforeOrEqual(calendarDate: CalendarDate): boolean; isAfterOrEqual(calendarDate: CalendarDate): boolean; /** * Returns a new CalendarDate with the specified amount of months added. * * @param amount * @param enforceEndOfMonth If set to true the addition will never cause an overflow to the next month. */ addMonths(amount: number, enforceEndOfMonth?: boolean): CalendarDate; /** * Returns a new CalendarDate with the specified amount of days added. * Allows overflow. */ addDays(amount: number): CalendarDate; getLastDayOfMonth(): CalendarDate; getFirstDayOfMonth(): CalendarDate; isFirstDayOfMonth(): boolean; isLastDayOfMonth(): boolean; /** * returns the last day (sunday) of the week of this calendar date as a new calendar date object. */ getLastDayOfWeek(): CalendarDate; /** * returns the first day (monday) of the week of this calendar date as a new calendar date object. */ getFirstDayOfWeek(): CalendarDate; /** * returns true if the weekday is monday(1) */ isFirstDayOfWeek(): boolean; /** * returns true if the weekday is sunday(7) */ isLastDayOfWeek(): boolean; /** * subtracts the input CalendarDate from this CalendarDate and returns the difference in days */ getDifferenceInDays(calendarDate: CalendarDate, absolute?: boolean): number; /** * The number of the week of the year that the day is in. By definition (ISO 8601), the first week of a year contains January 4 of that year. * (The ISO-8601 week starts on Monday.) In other words, the first Thursday of a year is in week 1 of that year. (for timestamp values only). * [source: https://www.postgresql.org/docs/8.1/functions-datetime.html] */ get week(): number; /** * Source: https://weeknumber.com/how-to/javascript */ private static getWeekNumber; /** * The quarter of the year (1-4) based on the month of the date. * * Quarter breakdown: * - 1st Quarter: January to March (1-3) * - 2nd Quarter: April to June (4-6) * - 3rd Quarter: July to September (7-9) * - 4th Quarter: October to December (10-12) */ get quarter(): number; }