UNPKG

11.4 kBTypeScriptView Raw
1import type { EndOfWeekOptions, StartOfWeekOptions, FormatOptions as DateFnsFormatOptions, GetWeekOptions, Interval } from "date-fns";
2import type { Locale } from "date-fns/locale";
3import { Numerals } from "../types/shared.js";
4export type { Locale } from "date-fns/locale";
5export type { Month as DateFnsMonth } from "date-fns";
6/**
7 * @ignore
8 * @deprecated Use {@link DateLibOptions} instead.
9 */
10export type FormatOptions = DateLibOptions;
11/**
12 * @ignore
13 * @deprecated Use {@link DateLibOptions} instead.
14 */
15export type LabelOptions = DateLibOptions;
16/**
17 * The options for the `DateLib` class.
18 *
19 * Extends `date-fns` [format](https://date-fns.org/docs/format),
20 * [startOfWeek](https://date-fns.org/docs/startOfWeek) and
21 * [endOfWeek](https://date-fns.org/docs/endOfWeek) options.
22 *
23 * @since 9.2.0
24 */
25export interface DateLibOptions extends DateFnsFormatOptions, StartOfWeekOptions, EndOfWeekOptions {
26 /** A constructor for the `Date` object. */
27 Date?: typeof Date;
28 /** A locale to use for formatting dates. */
29 locale?: Locale;
30 /**
31 * A time zone to use for dates.
32 *
33 * @since 9.5.0
34 */
35 timeZone?: string;
36 /**
37 * The numbering system to use for formatting numbers.
38 *
39 * @since 9.5.0
40 */
41 numerals?: Numerals;
42}
43/**
44 * A wrapper class around [date-fns](http://date-fns.org) sharing the same
45 * options.
46 *
47 * @since 9.2.0
48 * @example
49 * const dateLib = new DateLib({ locale: es });
50 * const newDate = dateLib.addDays(new Date(), 5);
51 */
52export declare class DateLib {
53 /** The options for the date library. */
54 readonly options: DateLibOptions;
55 /** Overrides for the date library functions. */
56 readonly overrides?: Partial<typeof DateLib.prototype>;
57 /**
58 * Creates an instance of DateLib.
59 *
60 * @param options The options for the date library.
61 * @param overrides Overrides for the date library functions.
62 */
63 constructor(options?: DateLibOptions, overrides?: Partial<typeof DateLib.prototype>);
64 /**
65 * Generate digit map dynamically using Intl.NumberFormat.
66 *
67 * @since 9.5.0
68 */
69 private getDigitMap;
70 /**
71 * Replace Arabic digits with the target numbering system digits.
72 *
73 * @since 9.5.0
74 */
75 private replaceDigits;
76 /**
77 * Format number using the custom numbering system.
78 *
79 * @since 9.5.0
80 * @param value The number to format.
81 * @returns The formatted number.
82 */
83 formatNumber(value: number): string;
84 /**
85 * Reference to the built-in Date constructor.
86 *
87 * @deprecated Use `newDate()` or `today()`.
88 */
89 Date: typeof Date;
90 /**
91 * Creates a new date object to the today's date.
92 *
93 * @since 9.5.0
94 * @returns The new date object.
95 */
96 today: () => Date;
97 /**
98 * Creates a new date object with the specified year, month and date.
99 *
100 * @since 9.5.0
101 * @param year The year.
102 * @param monthIndex The month (0-11).
103 * @param date The day of the month.
104 * @returns The new date object.
105 */
106 newDate: (year: number, monthIndex: number, date: number) => Date;
107 /**
108 * Adds the specified number of days to the given date.
109 *
110 * @param date The date to add days to.
111 * @param amount The number of days to add.
112 * @returns The new date with the days added.
113 */
114 addDays: (date: Date, amount: number) => Date;
115 /**
116 * Adds the specified number of months to the given date.
117 *
118 * @param date The date to add months to.
119 * @param amount The number of months to add.
120 * @returns The new date with the months added.
121 */
122 addMonths: (date: Date, amount: number) => Date;
123 /**
124 * Adds the specified number of weeks to the given date.
125 *
126 * @param date The date to add weeks to.
127 * @param amount The number of weeks to add.
128 * @returns The new date with the weeks added.
129 */
130 addWeeks: (date: Date, amount: number) => Date;
131 /**
132 * Adds the specified number of years to the given date.
133 *
134 * @param date The date to add years to.
135 * @param amount The number of years to add.
136 * @returns The new date with the years added.
137 */
138 addYears: (date: Date, amount: number) => Date;
139 /**
140 * Returns the number of calendar days between the given dates.
141 *
142 * @param dateLeft The later date.
143 * @param dateRight The earlier date.
144 * @returns The number of calendar days between the dates.
145 */
146 differenceInCalendarDays: (dateLeft: Date, dateRight: Date) => number;
147 /**
148 * Returns the number of calendar months between the given dates.
149 *
150 * @param dateLeft The later date.
151 * @param dateRight The earlier date.
152 * @returns The number of calendar months between the dates.
153 */
154 differenceInCalendarMonths: (dateLeft: Date, dateRight: Date) => number;
155 /**
156 * Returns the months between the given dates.
157 *
158 * @param interval The interval to get the months for.
159 */
160 eachMonthOfInterval: (interval: Interval<Date>) => Date[];
161 /**
162 * Returns the end of the broadcast week for the given date.
163 *
164 * @param date The original date.
165 * @returns The end of the broadcast week.
166 */
167 endOfBroadcastWeek: (date: Date, dateLib?: DateLib) => Date;
168 /**
169 * Returns the end of the ISO week for the given date.
170 *
171 * @param date The original date.
172 * @returns The end of the ISO week.
173 */
174 endOfISOWeek: (date: Date) => Date;
175 /**
176 * Returns the end of the month for the given date.
177 *
178 * @param date The original date.
179 * @returns The end of the month.
180 */
181 endOfMonth: (date: Date) => Date;
182 /**
183 * Returns the end of the week for the given date.
184 *
185 * @param date The original date.
186 * @returns The end of the week.
187 */
188 endOfWeek: (date: Date, options?: EndOfWeekOptions<Date>) => Date;
189 /**
190 * Returns the end of the year for the given date.
191 *
192 * @param date The original date.
193 * @returns The end of the year.
194 */
195 endOfYear: (date: Date) => Date;
196 /**
197 * Formats the given date using the specified format string.
198 *
199 * @param date The date to format.
200 * @param formatStr The format string.
201 * @returns The formatted date string.
202 */
203 format: (date: Date, formatStr: string, options?: DateFnsFormatOptions) => string;
204 /**
205 * Returns the ISO week number for the given date.
206 *
207 * @param date The date to get the ISO week number for.
208 * @returns The ISO week number.
209 */
210 getISOWeek: (date: Date) => number;
211 /**
212 * Returns the month of the given date.
213 *
214 * @param date The date to get the month for.
215 * @returns The month.
216 */
217 getMonth: (date: Date) => number;
218 /**
219 * Returns the year of the given date.
220 *
221 * @param date The date to get the year for.
222 * @returns The year.
223 */
224 getYear: (date: Date) => number;
225 /**
226 * Returns the local week number for the given date.
227 *
228 * @param date The date to get the week number for.
229 * @returns The week number.
230 */
231 getWeek: (date: Date, options?: GetWeekOptions) => number;
232 /**
233 * Checks if the first date is after the second date.
234 *
235 * @param date The date to compare.
236 * @param dateToCompare The date to compare with.
237 * @returns True if the first date is after the second date.
238 */
239 isAfter: (date: Date, dateToCompare: Date) => boolean;
240 /**
241 * Checks if the first date is before the second date.
242 *
243 * @param date The date to compare.
244 * @param dateToCompare The date to compare with.
245 * @returns True if the first date is before the second date.
246 */
247 isBefore: (date: Date, dateToCompare: Date) => boolean;
248 /**
249 * Checks if the given value is a Date object.
250 *
251 * @param value The value to check.
252 * @returns True if the value is a Date object.
253 */
254 isDate: (value: unknown) => value is Date;
255 /**
256 * Checks if the given dates are on the same day.
257 *
258 * @param dateLeft The first date to compare.
259 * @param dateRight The second date to compare.
260 * @returns True if the dates are on the same day.
261 */
262 isSameDay: (dateLeft: Date, dateRight: Date) => boolean;
263 /**
264 * Checks if the given dates are in the same month.
265 *
266 * @param dateLeft The first date to compare.
267 * @param dateRight The second date to compare.
268 * @returns True if the dates are in the same month.
269 */
270 isSameMonth: (dateLeft: Date, dateRight: Date) => boolean;
271 /**
272 * Checks if the given dates are in the same year.
273 *
274 * @param dateLeft The first date to compare.
275 * @param dateRight The second date to compare.
276 * @returns True if the dates are in the same year.
277 */
278 isSameYear: (dateLeft: Date, dateRight: Date) => boolean;
279 /**
280 * Returns the latest date in the given array of dates.
281 *
282 * @param dates The array of dates to compare.
283 * @returns The latest date.
284 */
285 max: (dates: Date[]) => Date;
286 /**
287 * Returns the earliest date in the given array of dates.
288 *
289 * @param dates The array of dates to compare.
290 * @returns The earliest date.
291 */
292 min: (dates: Date[]) => Date;
293 /**
294 * Sets the month of the given date.
295 *
296 * @param date The date to set the month on.
297 * @param month The month to set (0-11).
298 * @returns The new date with the month set.
299 */
300 setMonth: (date: Date, month: number) => Date;
301 /**
302 * Sets the year of the given date.
303 *
304 * @param date The date to set the year on.
305 * @param year The year to set.
306 * @returns The new date with the year set.
307 */
308 setYear: (date: Date, year: number) => Date;
309 /**
310 * Returns the start of the broadcast week for the given date.
311 *
312 * @param date The original date.
313 * @returns The start of the broadcast week.
314 */
315 startOfBroadcastWeek: (date: Date, dateLib?: DateLib) => Date;
316 /**
317 * Returns the start of the day for the given date.
318 *
319 * @param date The original date.
320 * @returns The start of the day.
321 */
322 startOfDay: (date: Date) => Date;
323 /**
324 * Returns the start of the ISO week for the given date.
325 *
326 * @param date The original date.
327 * @returns The start of the ISO week.
328 */
329 startOfISOWeek: (date: Date) => Date;
330 /**
331 * Returns the start of the month for the given date.
332 *
333 * @param date The original date.
334 * @returns The start of the month.
335 */
336 startOfMonth: (date: Date) => Date;
337 /**
338 * Returns the start of the week for the given date.
339 *
340 * @param date The original date.
341 * @returns The start of the week.
342 */
343 startOfWeek: (date: Date) => Date;
344 /**
345 * Returns the start of the year for the given date.
346 *
347 * @param date The original date.
348 * @returns The start of the year.
349 */
350 startOfYear: (date: Date) => Date;
351}
352/** The default locale (English). */
353export { enUS as defaultLocale } from "date-fns/locale/en-US";
354/**
355 * The default date library with English locale.
356 *
357 * @since 9.2.0
358 */
359export declare const defaultDateLib: DateLib;
360/**
361 * @ignore
362 * @deprecated Use `defaultDateLib`.
363 */
364export declare const dateLib: DateLib;