UNPKG

20.6 kBTypeScriptView Raw
1import { addDays } from "date-fns/addDays";
2import { addHours } from "date-fns/addHours";
3import { addMinutes } from "date-fns/addMinutes";
4import { addMonths } from "date-fns/addMonths";
5import { addQuarters } from "date-fns/addQuarters";
6import { addSeconds } from "date-fns/addSeconds";
7import { addWeeks } from "date-fns/addWeeks";
8import { addYears } from "date-fns/addYears";
9import { getDate } from "date-fns/getDate";
10import { getDay } from "date-fns/getDay";
11import { getHours } from "date-fns/getHours";
12import { getMinutes } from "date-fns/getMinutes";
13import { getMonth } from "date-fns/getMonth";
14import { getQuarter } from "date-fns/getQuarter";
15import { getSeconds } from "date-fns/getSeconds";
16import { getTime } from "date-fns/getTime";
17import { getYear } from "date-fns/getYear";
18import { isAfter } from "date-fns/isAfter";
19import { isBefore } from "date-fns/isBefore";
20import { isDate } from "date-fns/isDate";
21import { set } from "date-fns/set";
22import { setHours } from "date-fns/setHours";
23import { setMinutes } from "date-fns/setMinutes";
24import { setMonth } from "date-fns/setMonth";
25import { setQuarter } from "date-fns/setQuarter";
26import { setYear } from "date-fns/setYear";
27import { subDays } from "date-fns/subDays";
28import { subMonths } from "date-fns/subMonths";
29import { subQuarters } from "date-fns/subQuarters";
30import { subWeeks } from "date-fns/subWeeks";
31import { subYears } from "date-fns/subYears";
32import type { Day, Locale as DateFnsLocale } from "date-fns";
33export type DateNumberType = Day;
34interface LocaleObj extends Pick<DateFnsLocale, "options" | "formatLong" | "localize" | "match"> {
35}
36export type Locale = string | LocaleObj;
37export declare enum KeyType {
38 ArrowUp = "ArrowUp",
39 ArrowDown = "ArrowDown",
40 ArrowLeft = "ArrowLeft",
41 ArrowRight = "ArrowRight",
42 PageUp = "PageUp",
43 PageDown = "PageDown",
44 Home = "Home",
45 End = "End",
46 Enter = "Enter",
47 Space = " ",
48 Tab = "Tab",
49 Escape = "Escape",
50 Backspace = "Backspace",
51 X = "x"
52}
53export declare const DEFAULT_YEAR_ITEM_NUMBER = 12;
54export declare function newDate(value?: string | Date | number | null): Date;
55/**
56 * Parses a date.
57 *
58 * @param value - The string representing the Date in a parsable form, e.g., ISO 1861
59 * @param dateFormat - The date format.
60 * @param locale - The locale.
61 * @param strictParsing - The strict parsing flag.
62 * @param minDate - The minimum date.
63 * @returns - The parsed date or null.
64 */
65export declare function parseDate(value: string, dateFormat: string | string[], locale: Locale | undefined, strictParsing: boolean, minDate?: Date): Date | null;
66export { isDate, set };
67/**
68 * Checks if a given date is valid and not before the minimum date.
69 * @param date - The date to be checked.
70 * @param minDate - The minimum date allowed. If not provided, defaults to "1/1/1800".
71 * @returns A boolean value indicating whether the date is valid and not before the minimum date.
72 */
73export declare function isValid(date: Date, minDate?: Date): boolean;
74/**
75 * Formats a date.
76 *
77 * @param date - The date.
78 * @param formatStr - The format string.
79 * @param locale - The locale.
80 * @returns - The formatted date.
81 */
82export declare function formatDate(date: Date, formatStr: string, locale?: Locale): string;
83/**
84 * Safely formats a date.
85 *
86 * @param date - The date.
87 * @param options - An object containing the dateFormat and locale.
88 * @returns - The formatted date or an empty string.
89 */
90export declare function safeDateFormat(date: Date | null | undefined, { dateFormat, locale }: {
91 dateFormat: string | string[];
92 locale?: Locale;
93}): string;
94/**
95 * Safely formats a date range.
96 *
97 * @param startDate - The start date.
98 * @param endDate - The end date.
99 * @param props - The props.
100 * @returns - The formatted date range or an empty string.
101 */
102export declare function safeDateRangeFormat(startDate: Date | null | undefined, endDate: Date | null | undefined, props: {
103 dateFormat: string | string[];
104 locale?: Locale;
105}): string;
106/**
107 * Safely formats multiple dates.
108 *
109 * @param dates - The dates.
110 * @param props - The props.
111 * @returns - The formatted dates or an empty string.
112 */
113export declare function safeMultipleDatesFormat(dates: Date[], props: {
114 dateFormat: string | string[];
115 locale?: Locale;
116}): string;
117/**
118 * Sets the time for a given date.
119 *
120 * @param date - The date.
121 * @param time - An object containing the hour, minute, and second.
122 * @returns - The date with the time set.
123 */
124export declare function setTime(date: Date, { hour, minute, second }: {
125 hour?: number | undefined;
126 minute?: number | undefined;
127 second?: number | undefined;
128}): Date;
129export { setMinutes, setHours, setMonth, setQuarter, setYear };
130export { getSeconds, getMinutes, getHours, getMonth, getQuarter, getYear, getDay, getDate, getTime, };
131/**
132 * Gets the week of the year for a given date.
133 *
134 * @param date - The date.
135 * @returns - The week of the year.
136 */
137export declare function getWeek(date: Date): number;
138/**
139 * Gets the day of the week code for a given day.
140 *
141 * @param day - The day.
142 * @param locale - The locale.
143 * @returns - The day of the week code.
144 */
145export declare function getDayOfWeekCode(day: Date, locale?: Locale): string;
146/**
147 * Gets the start of the day for a given date.
148 *
149 * @param date - The date.
150 * @returns - The start of the day.
151 */
152export declare function getStartOfDay(date: Date): Date;
153/**
154 * Gets the start of the week for a given date.
155 *
156 * @param date - The date.
157 * @param locale - The locale.
158 * @param calendarStartDay - The day the calendar starts on.
159 * @returns - The start of the week.
160 */
161export declare function getStartOfWeek(date: Date, locale?: Locale, calendarStartDay?: Day): Date;
162/**
163 * Gets the start of the month for a given date.
164 *
165 * @param date - The date.
166 * @returns - The start of the month.
167 */
168export declare function getStartOfMonth(date: Date): Date;
169/**
170 * Gets the start of the year for a given date.
171 *
172 * @param date - The date.
173 * @returns - The start of the year.
174 */
175export declare function getStartOfYear(date: Date): Date;
176/**
177 * Gets the start of the quarter for a given date.
178 *
179 * @param date - The date.
180 * @returns - The start of the quarter.
181 */
182export declare function getStartOfQuarter(date: Date): Date;
183/**
184 * Gets the start of today.
185 *
186 * @returns - The start of today.
187 */
188export declare function getStartOfToday(): Date;
189/**
190 * Gets the end of the day for a given date.
191 *
192 * @param date - The date.
193 * @returns - The end of the day.
194 */
195export declare function getEndOfDay(date: Date): Date;
196/**
197 * Gets the end of the week for a given date.
198 *
199 * @param date - The date.
200 * @returns - The end of the week.
201 */
202export declare function getEndOfWeek(date: Date): Date;
203/**
204 * Gets the end of the month for a given date.
205 *
206 * @param date - The date.
207 * @returns - The end of the month.
208 */
209export declare function getEndOfMonth(date: Date): Date;
210export { addSeconds, addMinutes, addDays, addWeeks, addMonths, addQuarters, addYears, };
211export { addHours, subDays, subWeeks, subMonths, subQuarters, subYears };
212export { isBefore, isAfter };
213/**
214 * Checks if two dates are in the same year.
215 *
216 * @param date1 - The first date.
217 * @param date2 - The second date.
218 * @returns - True if the dates are in the same year, false otherwise.
219 */
220export declare function isSameYear(date1: Date | null, date2: Date | null): boolean;
221/**
222 * Checks if two dates are in the same month.
223 *
224 * @param date1 - The first date.
225 * @param date2 - The second date.
226 * @returns - True if the dates are in the same month, false otherwise.
227 */
228export declare function isSameMonth(date1: Date | null, date2?: Date | null): boolean;
229/**
230 * Checks if two dates are in the same quarter.
231 *
232 * @param date1 - The first date.
233 * @param date2 - The second date.
234 * @returns - True if the dates are in the same quarter, false otherwise.
235 */
236export declare function isSameQuarter(date1: Date | null, date2: Date | null): boolean;
237/**
238 * Checks if two dates are on the same day.
239 *
240 * @param date1 - The first date.
241 * @param date2 - The second date.
242 * @returns - True if the dates are on the same day, false otherwise.
243 */
244export declare function isSameDay(date1?: Date | null, date2?: Date | null): boolean;
245/**
246 * Checks if two dates are equal.
247 *
248 * @param date1 - The first date.
249 * @param date2 - The second date.
250 * @returns - True if the dates are equal, false otherwise.
251 */
252export declare function isEqual(date1: Date | null | undefined, date2: Date | null | undefined): boolean;
253/**
254 * Checks if a day is within a date range.
255 *
256 * @param day - The day to check.
257 * @param startDate - The start date of the range.
258 * @param endDate - The end date of the range.
259 * @returns - True if the day is within the range, false otherwise.
260 */
261export declare function isDayInRange(day: Date, startDate: Date, endDate: Date): boolean;
262/**
263 * Gets the difference in days between two dates.
264 *
265 * @param date1 - The first date.
266 * @param date2 - The second date.
267 * @returns - The difference in days.
268 */
269export declare function getDaysDiff(date1: Date, date2: Date): number;
270/**
271 * Registers a locale.
272 *
273 * @param localeName - The name of the locale.
274 * @param localeData - The data of the locale.
275 */
276export declare function registerLocale(localeName: string, localeData: LocaleObj): void;
277/**
278 * Sets the default locale.
279 *
280 * @param localeName - The name of the locale.
281 */
282export declare function setDefaultLocale(localeName?: string): void;
283/**
284 * Gets the default locale.
285 *
286 * @returns - The default locale.
287 */
288export declare function getDefaultLocale(): string | undefined;
289/**
290 * Gets the locale object.
291 *
292 * @param localeSpec - The locale specification.
293 * @returns - The locale object.
294 */
295export declare function getLocaleObject(localeSpec?: Locale): LocaleObj | undefined;
296/**
297 * Formats the weekday in a given locale.
298 *
299 * @param date - The date to format.
300 * @param formatFunc - The formatting function.
301 * @param locale - The locale to use for formatting.
302 * @returns - The formatted weekday.
303 */
304export declare function getFormattedWeekdayInLocale(date: Date, formatFunc: (date: string) => string, locale?: Locale): string;
305/**
306 * Gets the minimum weekday in a given locale.
307 *
308 * @param date - The date to format.
309 * @param locale - The locale to use for formatting.
310 * @returns - The minimum weekday.
311 */
312export declare function getWeekdayMinInLocale(date: Date, locale?: Locale): string;
313/**
314 * Gets the short weekday in a given locale.
315 *
316 * @param date - The date to format.
317 * @param locale - The locale to use for formatting.
318 * @returns - The short weekday.
319 */
320export declare function getWeekdayShortInLocale(date: Date, locale?: Locale): string;
321/**
322 * Gets the month in a given locale.
323 *
324 * @param month - The month to format.
325 * @param locale - The locale to use for formatting.
326 * @returns - The month.
327 */
328export declare function getMonthInLocale(month: number, locale?: Locale): string;
329/**
330 * Gets the short month in a given locale.
331 *
332 * @param month - The month to format.
333 * @param locale - The locale to use for formatting.
334 * @returns - The short month.
335 */
336export declare function getMonthShortInLocale(month: number, locale?: Locale): string;
337/**
338 * Gets the short quarter in a given locale.
339 *
340 * @param quarter - The quarter to format.
341 * @param locale - The locale to use for formatting.
342 * @returns - The short quarter.
343 */
344export declare function getQuarterShortInLocale(quarter: number, locale?: Locale): string;
345export interface DateFilterOptions {
346 minDate?: Date;
347 maxDate?: Date;
348 excludeDates?: {
349 date: Date;
350 message?: string;
351 }[] | Date[];
352 excludeDateIntervals?: {
353 start: Date;
354 end: Date;
355 }[];
356 includeDates?: Date[];
357 includeDateIntervals?: {
358 start: Date;
359 end: Date;
360 }[];
361 filterDate?: (date: Date) => boolean;
362 yearItemNumber?: number;
363}
364/**
365 * Checks if a day is disabled.
366 *
367 * @param day - The day to check.
368 * @param options - The options to consider when checking.
369 * @returns - Returns true if the day is disabled, false otherwise.
370 */
371export declare function isDayDisabled(day: Date, { minDate, maxDate, excludeDates, excludeDateIntervals, includeDates, includeDateIntervals, filterDate, }?: DateFilterOptions): boolean;
372/**
373 * Checks if a day is excluded.
374 *
375 * @param day - The day to check.
376 * @param options - The options to consider when checking.
377 * @returns - Returns true if the day is excluded, false otherwise.
378 */
379export declare function isDayExcluded(day: Date, { excludeDates, excludeDateIntervals, }?: Pick<DateFilterOptions, "excludeDates" | "excludeDateIntervals">): boolean;
380export declare function isMonthDisabled(month: Date, { minDate, maxDate, excludeDates, includeDates, filterDate, }?: Pick<DateFilterOptions, "minDate" | "maxDate" | "excludeDates" | "includeDates" | "filterDate">): boolean;
381export declare function isMonthInRange(startDate: Date, endDate: Date, m: number, day: Date): boolean;
382/**
383 * To check if a date's month and year are disabled/excluded
384 * @param date Date to check
385 * @returns {boolean} true if month and year are disabled/excluded, false otherwise
386 */
387export declare function isMonthYearDisabled(date: Date, { minDate, maxDate, excludeDates, includeDates, }?: Pick<DateFilterOptions, "minDate" | "maxDate" | "excludeDates" | "includeDates">): boolean;
388export declare function isQuarterDisabled(quarter: Date, { minDate, maxDate, excludeDates, includeDates, filterDate, }?: Pick<DateFilterOptions, "minDate" | "maxDate" | "excludeDates" | "includeDates" | "filterDate">): boolean;
389export declare function isYearInRange(year: number, start?: Date | null, end?: Date | null): boolean;
390export declare function isYearDisabled(year: number, { minDate, maxDate, excludeDates, includeDates, filterDate, }?: Pick<DateFilterOptions, "minDate" | "maxDate" | "excludeDates" | "includeDates" | "filterDate">): boolean;
391export declare function isQuarterInRange(startDate: Date, endDate: Date, q: number, day: Date): boolean;
392export declare function isOutOfBounds(day: Date, { minDate, maxDate }?: Pick<DateFilterOptions, "minDate" | "maxDate">): boolean;
393export declare function isTimeInList(time: Date, times: Date[]): boolean;
394export interface TimeFilterOptions {
395 minTime?: Date;
396 maxTime?: Date;
397 excludeTimes?: Date[];
398 includeTimes?: Date[];
399 filterTime?: (time: Date) => boolean;
400}
401export declare function isTimeDisabled(time: Date, { excludeTimes, includeTimes, filterTime, }?: Pick<TimeFilterOptions, "excludeTimes" | "includeTimes" | "filterTime">): boolean;
402export declare function isTimeInDisabledRange(time: Date, { minTime, maxTime }: Pick<TimeFilterOptions, "minTime" | "maxTime">): boolean;
403export declare function monthDisabledBefore(day: Date, { minDate, includeDates, }?: Pick<DateFilterOptions, "minDate" | "includeDates">): boolean;
404export declare function monthDisabledAfter(day: Date, { maxDate, includeDates, }?: Pick<DateFilterOptions, "maxDate" | "includeDates">): boolean;
405export declare function quarterDisabledBefore(date: Date, { minDate, includeDates, }?: Pick<DateFilterOptions, "minDate" | "includeDates">): boolean;
406export declare function quarterDisabledAfter(date: Date, { maxDate, includeDates, }?: Pick<DateFilterOptions, "maxDate" | "includeDates">): boolean;
407export declare function yearDisabledBefore(day: Date, { minDate, includeDates, }?: Pick<DateFilterOptions, "minDate" | "includeDates">): boolean;
408export declare function yearsDisabledBefore(day: Date, { minDate, yearItemNumber, }?: Pick<DateFilterOptions, "minDate" | "yearItemNumber">): boolean;
409export declare function yearDisabledAfter(day: Date, { maxDate, includeDates, }?: Pick<DateFilterOptions, "maxDate" | "includeDates">): boolean;
410export declare function yearsDisabledAfter(day: Date, { maxDate, yearItemNumber, }?: Pick<DateFilterOptions, "maxDate" | "yearItemNumber">): boolean;
411export declare function getEffectiveMinDate({ minDate, includeDates, }: Pick<DateFilterOptions, "minDate" | "includeDates">): Date | undefined;
412export declare function getEffectiveMaxDate({ maxDate, includeDates, }: Pick<DateFilterOptions, "maxDate" | "includeDates">): Date | undefined;
413export interface HighlightDate {
414 [className: string]: Date[];
415}
416/**
417 * Get a map of highlighted dates with their corresponding classes.
418 * @param highlightDates The dates to highlight.
419 * @param defaultClassName The default class to use for highlighting.
420 * @returns A map with dates as keys and arrays of class names as values.
421 */
422export declare function getHighLightDaysMap(highlightDates?: (Date | HighlightDate)[], defaultClassName?: string): Map<string, string[]>;
423/**
424 * Compare the two arrays
425 * @param array1 The first array to compare.
426 * @param array2 The second array to compare.
427 * @returns true, if the passed arrays are equal, false otherwise.
428 */
429export declare function arraysAreEqual<T>(array1: T[], array2: T[]): boolean;
430export interface HolidayItem {
431 date: Date;
432 holidayName: string;
433}
434interface ClassNamesObj {
435 className: string;
436 holidayNames: string[];
437}
438export type HolidaysMap = Map<string, ClassNamesObj>;
439/**
440 * Assign the custom class to each date
441 * @param holidayDates array of object containing date and name of the holiday
442 * @param defaultClassName className to be added.
443 * @returns Map containing date as key and array of className and holiday name as value
444 */
445export declare function getHolidaysMap(holidayDates?: HolidayItem[], defaultClassName?: string): HolidaysMap;
446/**
447 * Determines the times to inject after a given start of day, current time, and multiplier.
448 * @param startOfDay The start of the day.
449 * @param currentTime The current time.
450 * @param currentMultiplier The current multiplier.
451 * @param intervals The intervals.
452 * @param injectedTimes The times to potentially inject.
453 * @returns An array of times to inject.
454 */
455export declare function timesToInjectAfter(startOfDay: Date, currentTime: Date, currentMultiplier: number, intervals: number, injectedTimes: Date[]): Date[];
456/**
457 * Adds a leading zero to a number if it's less than 10.
458 * @param i The number to add a leading zero to.
459 * @returns The number as a string, with a leading zero if it was less than 10.
460 */
461export declare function addZero(i: number): string;
462/**
463 * Gets the start and end years for a period.
464 * @param date The date to get the period for.
465 * @param yearItemNumber The number of years in the period. Defaults to DEFAULT_YEAR_ITEM_NUMBER.
466 * @returns An object with the start and end years for the period.
467 */
468export declare function getYearsPeriod(date: Date, yearItemNumber?: number): {
469 startPeriod: number;
470 endPeriod: number;
471};
472/**
473 * Gets the number of hours in a day.
474 * @param d The date to get the number of hours for.
475 * @returns The number of hours in the day.
476 */
477export declare function getHoursInDay(d: Date): number;
478/**
479 * Returns the start of the minute for the given date
480 *
481 * NOTE: this function is a DST and timezone-safe analog of `date-fns/startOfMinute`
482 * do not make changes unless you know what you're doing
483 *
484 * See comments on https://github.com/Hacker0x01/react-datepicker/pull/4244
485 * for more details
486 *
487 * @param d date
488 * @returns start of the minute
489 */
490export declare function startOfMinute(d: Date): Date;
491/**
492 * Returns whether the given dates are in the same minute
493 *
494 * This function is a DST and timezone-safe analog of `date-fns/isSameMinute`
495 *
496 * @param d1
497 * @param d2
498 * @returns
499 */
500export declare function isSameMinute(d1: Date, d2: Date): boolean;
501/**
502 * Returns a new datetime object representing the input date with midnight time
503 * @param date The date to get the midnight time for
504 * @returns A new datetime object representing the input date with midnight time
505 */
506export declare function getMidnightDate(date: Date): Date;
507/**
508 * Is the first date before the second one?
509 * @param date The date that should be before the other one to return true
510 * @param dateToCompare The date to compare with
511 * @returns The first date is before the second date
512 *
513 * Note:
514 * This function considers the mid-night of the given dates for comparison.
515 * It evaluates whether date is before dateToCompare based on their mid-night timestamps.
516 */
517export declare function isDateBefore(date: Date, dateToCompare: Date): boolean;
518/**
519 * Checks if the space key was pressed down.
520 *
521 * @param event - The keyboard event.
522 * @returns - Returns true if the space key was pressed down, false otherwise.
523 */
524export declare function isSpaceKeyDown(event: React.KeyboardEvent<HTMLDivElement>): boolean;