UNPKG

atomic-fns

Version:

Like Lodash, but for ESNext and with types. Stop shipping code built for browsers from 2015.

205 lines (204 loc) 8.84 kB
import { Duration, DurationUnit, TDuration } from '../duration.js'; import { asDate, formatDate, strftime } from '../format.js'; import { DateObject, daysInMonth, daysInYear, isLeapYear, maxDate, minDate, weeksInYear } from './utils.js'; export { asDate, daysInMonth, daysInYear, formatDate, isLeapYear, maxDate, minDate, strftime, weeksInYear }; export { DateObject }; export declare type DateInput = number | string | Date; export declare type DateLike = DateInput | IntlDate | DateObject; /** * Creates a date tied to a given locale (default system locale) which can be formatted in that locale's language using the native {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl Intl Apis} directly or using a formatting string. * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl Intl Apis} */ export declare class IntlDate { private _date; readonly locale?: string; intlRelativeFormat: Intl.RelativeTimeFormat; /** Parses the provided value as UTC date or returns the current UTC date. */ static UTC(value?: DateLike, locale?: string): IntlDate; /** Returns the current local date and time.*/ static now(): IntlDate; /** Creates a new date from a Unix timestamp (seconds since the unix epoch) */ static unix(seconds: number): IntlDate; /** * Creates a new `IntlDate` in local time and specified locale (default is system locale). * **Note:** The allowed values for `month` start at 1, which is different from legacy `Date` * @param {DateLike} obj The date value (default is current local time) * @param {Object} opts The options for this date * @param {boolean?} opts.utc Converts input or current time to UTC * @param {string?} opts.locale A locale string to use for this date * @returns */ constructor(obj?: DateLike, { locale, utc }?: any); /** Get the date's year. */ get year(): number; /** Get the this date's month as a number from 1 to 12, inclusive. */ get month(): number; /** Returns the weekday as a number between 1 and 7, inclusive, where Monday is 1 and Sunday is 7. */ get dayOfWeek(): number; /** Get the this date's current day of the month. */ get day(): number; /** Get the this date's current hour. */ get hour(): number; /** Get the this date's current minute. */ get minute(): number; /** Get the this date's current second. */ get second(): number; /** Get the this date's current millisecond. */ get millisecond(): number; isValid(): boolean; /** Get the number of days in this date's month. */ daysInMonth(): number; /** Get the number of days in this date's year. */ daysInYear(): number; dayOfYear(): number; /** Returns `true` if this date's year is a leap year. */ isLeapYear(): boolean; /** Gets the number of weeks according to locale in the current year. */ weeksInYear(): number; /** Gets the number of weeks in the current year, according to ISO weeks. */ isoWeeksInYear(): number; /** Returns the current week of the year. */ week(): any; /** Returns the ISO week of the year. */ isoWeek(): any; /** * Returns a localized string representation of this date. * @param {Intl.DateTimeFormatOptions} opts The options to use for the format * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat Intl.DateTimeFormat()} */ toString(opts?: Intl.DateTimeFormatOptions): string; /** * Returns a localized string representation of this date, according to the given format string. Format codes use the same specification as {@link https://momentjs.com/docs/#/displaying/format/ moment}. * @param {string} str The format string to use * @see {@link https://momentjs.com/docs/#/displaying/format/ List of formats} * @example ```js new IntlDate().format('MM/DD/YYYY') // '10/31/2022' ``` */ format(str: string): string; /** * Returns a new copy of the native `Date` object used by this instance. * @returns {Date} A new `Date` object */ toDate(): Date; /** * Returns the number of seconds since the Unix Epoch (January 1, 1970 UTC). * @see {@link IntlDate.timestamp} * @see {@link IntlDate.unix} * @example ```js new IntlDate().toSeconds() // 1318874398 ``` */ toSeconds(): number; /** * Returns the number of seconds since the Unix Epoch (January 1, 1970 UTC). * @see {@link IntlDate.toSeconds} * @see {@link IntlDate.unix} * @example ```js new IntlDate().timestamp() // 1318874398 ``` */ timestamp(): number; /** * Returns the number of seconds since the Unix Epoch (January 1, 1970 UTC). * @see {@link IntlDate.toSeconds} * @see {@link IntlDate.timestamp} * @example ```js new IntlDate().unix() // 1318874398 ``` */ unix(): number; /** Formats a string to the ISO8601 standard. * @example ```js new IntlDate().toISOString() // '2022-10-31T22:44:30.652Z' ``` */ toISOString(): string; /** Returns the date part formatted as ISO8601. * @example ```js new IntlDate().toISODate() // '2022-10-31' ``` */ toISODate(): string; /** Returns the time part formatted as ISO8601. * @example ```js new IntlDate().toISOTime() // 'T22:44:30.652Z' ``` */ toISOTime(): string; /** * Returns an object containing year, month, day-of-month, hours, minutes, seconds, milliseconds. * @returns {Object} An object like `{year, month, date, hours, minutes, seconds, ms}` */ toObject(): DateObject; /** Returns the number of milliseconds since the Unix Epoch (January 1, 1970 UTC) */ getTime(): number; /** Alias of {@link IntlDate.getTime} */ valueOf(): number; /** Returns the timezone string name */ zoneName(): string; /** Returns the timezone GMT offset as a string */ zone(): string; /** Returns the difference in `minutes` between this date and UTC */ utcOffset(): number; /** * Check if this date is before another date. The other value will be parsed as an `IntlDate` if not already so. * @param {DateLike} other Another date or date like object * @returns {boolean} Returns `true` if this date is before the given value */ isBefore(other: DateLike): boolean; /** * Check if this date is strictly after the given `start` and before `stop` dates. The values will be parsed as an `IntlDate` if not already so. * @param {DateLike} start The smaller date * @param {DateLike} stop The larger date * @returns {boolean} Returns `true` if this date is greater than `start` and less than `stop`. */ isBetween(start: DateLike, stop: DateLike): boolean; /** * Check if this date is after another date. The other value will be parsed as an `IntlDate` if not already so. * @param {DateLike} other Another date or date like object * @returns {boolean} Returns `true` if this date is after the given value */ isAfter(other: DateLike): boolean; /** * Check if this date is the same as `other`. * @param other Another date object * @returns {boolean} Returns `true` if this date is the same as other */ isSame(other: Date | IntlDate): boolean; lt(other: Date | IntlDate): boolean; eq(other: Date | IntlDate): boolean; compare(other: Date | IntlDate): number; set(values: DateObject): IntlDate; get relativeFormat(): Intl.RelativeTimeFormat; relativeTime(n: number, unit?: Intl.RelativeTimeFormatUnit): string; fromNow(unit?: Intl.RelativeTimeFormatUnit): string; from(other: DateLike, unit?: Intl.RelativeTimeFormatUnit): string; toNow(unit?: Intl.RelativeTimeFormatUnit): string; to(other: DateLike, unit?: Intl.RelativeTimeFormatUnit): string; diff(other: DateLike, unit?: DurationUnit, exact?: boolean): number; add(duration: TDuration, exact?: boolean): IntlDate; subtract(duration: TDuration, exact?: boolean): IntlDate; startOf(unit: DurationUnit): IntlDate; endOf(unit: DurationUnit): IntlDate; clone(date?: DateLike): IntlDate; static max(...args: any[]): any; static min(...args: any[]): any; inspect(): string; } export declare function addDuration(date: DateInput, duration: number | Duration | TDuration, exact?: boolean): Date; /** * Subtract the specified years, months, weeks, days, hours, minutes and seconds from the given date. * @param date * @param duration * @param exact * @returns */ export declare function subDuration(date: DateInput, duration: number | Duration | TDuration, exact?: boolean): Date;