@dereekb/date 13.0.7 → 13.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs.js +3526 -993
- package/index.esm.js +3515 -983
- package/package.json +5 -6
- package/src/lib/date/date.calendar.d.ts +63 -9
- package/src/lib/date/date.cell.d.ts +203 -104
- package/src/lib/date/date.cell.factory.d.ts +319 -86
- package/src/lib/date/date.cell.filter.d.ts +104 -2
- package/src/lib/date/date.cell.index.d.ts +202 -92
- package/src/lib/date/date.cell.schedule.d.ts +285 -102
- package/src/lib/date/date.cell.schedule.day.d.ts +13 -3
- package/src/lib/date/date.cell.validator.d.ts +6 -8
- package/src/lib/date/date.cell.week.d.ts +24 -3
- package/src/lib/date/date.d.ts +481 -54
- package/src/lib/date/date.day.d.ts +139 -49
- package/src/lib/date/date.duration.d.ts +49 -11
- package/src/lib/date/date.format.d.ts +355 -36
- package/src/lib/date/date.hashset.d.ts +11 -0
- package/src/lib/date/date.logical.d.ts +61 -3
- package/src/lib/date/date.range.d.ts +355 -77
- package/src/lib/date/date.range.string.d.ts +39 -0
- package/src/lib/date/date.range.timezone.d.ts +18 -6
- package/src/lib/date/date.round.d.ts +46 -1
- package/src/lib/date/date.rxjs.d.ts +29 -7
- package/src/lib/date/date.sort.d.ts +36 -9
- package/src/lib/date/date.time.d.ts +197 -26
- package/src/lib/date/date.time.limit.d.ts +67 -4
- package/src/lib/date/date.time.minute.d.ts +269 -30
- package/src/lib/date/date.timezone.d.ts +286 -70
- package/src/lib/date/date.unix.d.ts +3 -0
- package/src/lib/date/date.week.d.ts +115 -51
- package/src/lib/query/query.builder.d.ts +91 -0
- package/src/lib/query/query.builder.mongo.d.ts +50 -0
- package/src/lib/query/query.filter.d.ts +29 -2
- package/src/lib/query/query.request.d.ts +16 -0
- package/src/lib/rrule/date.recurrence.d.ts +66 -22
- package/src/lib/rrule/date.rrule.d.ts +131 -8
- package/src/lib/rrule/date.rrule.extension.d.ts +50 -9
- package/src/lib/rrule/date.rrule.parse.d.ts +85 -2
- package/src/lib/timezone/timezone.d.ts +102 -2
- package/src/lib/timezone/timezone.validator.d.ts +9 -4
package/src/lib/date/date.d.ts
CHANGED
|
@@ -1,115 +1,407 @@
|
|
|
1
1
|
import { type DayOfWeekNameFunction, type DateOrDateString, type ISO8601DateString, type Maybe, type Minutes, type Seconds, type TimezoneString, type ArrayOrValue, type MapFunction, type ISO8601DateStringUTCFull, type UTCDateString, type DayOfWeek, type UnixDateTimeMillisecondsNumber, type DateOrUnixDateTimeMillisecondsNumber, type DateHourMinuteOrSecond, type FloorOrCeilRounding } from '@dereekb/util';
|
|
2
|
+
/**
|
|
3
|
+
* Sentinel date representing the maximum future date (January 1, 9999 UTC).
|
|
4
|
+
*
|
|
5
|
+
* Used as a placeholder for "no expiration" or "indefinite" scenarios.
|
|
6
|
+
*/
|
|
2
7
|
export declare const MAX_FUTURE_DATE: Date;
|
|
3
8
|
/**
|
|
4
|
-
*
|
|
9
|
+
* Extracts a Date from an arbitrary input value.
|
|
5
10
|
*/
|
|
6
11
|
export type ReadDateFunction<T> = MapFunction<T, Date>;
|
|
7
12
|
/**
|
|
8
|
-
*
|
|
13
|
+
* Extracts an ISO8601 UTC full date string from an arbitrary input value.
|
|
9
14
|
*/
|
|
10
15
|
export type ReadISO8601DateStringUTCFullFunction<T> = MapFunction<T, ISO8601DateStringUTCFull>;
|
|
11
16
|
/**
|
|
12
|
-
*
|
|
17
|
+
* Type guard that checks whether the input is a Date instance.
|
|
18
|
+
*
|
|
19
|
+
* @param input - value to check
|
|
20
|
+
* @returns whether the input is a Date
|
|
13
21
|
*
|
|
14
|
-
* @
|
|
15
|
-
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* isDate(new Date()); // true
|
|
25
|
+
* isDate('2020-01-01'); // false
|
|
26
|
+
* ```
|
|
16
27
|
*/
|
|
17
28
|
export declare function isDate(input: unknown): input is Date;
|
|
29
|
+
/**
|
|
30
|
+
* Converts milliseconds to minutes.
|
|
31
|
+
*
|
|
32
|
+
* @param milliseconds - duration in milliseconds
|
|
33
|
+
* @returns equivalent duration in minutes
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* msToMinutes(60000); // 1
|
|
38
|
+
* msToMinutes(120000); // 2
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
18
41
|
export declare function msToMinutes(milliseconds: number): Minutes;
|
|
42
|
+
/**
|
|
43
|
+
* Converts milliseconds to seconds.
|
|
44
|
+
*
|
|
45
|
+
* @param milliseconds - duration in milliseconds
|
|
46
|
+
* @returns equivalent duration in seconds
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* msToSeconds(1000); // 1
|
|
51
|
+
* msToSeconds(2500); // 2.5
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
19
54
|
export declare function msToSeconds(milliseconds: number): Seconds;
|
|
55
|
+
/**
|
|
56
|
+
* Converts hours to milliseconds.
|
|
57
|
+
*
|
|
58
|
+
* @param hours - number of hours (defaults to 1)
|
|
59
|
+
* @returns equivalent duration in milliseconds
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* hoursToMs(1); // 3600000
|
|
64
|
+
* hoursToMs(2); // 7200000
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
20
67
|
export declare function hoursToMs(hours?: number): Minutes;
|
|
68
|
+
/**
|
|
69
|
+
* Converts minutes to milliseconds.
|
|
70
|
+
*
|
|
71
|
+
* @param minutes - number of minutes (defaults to 1)
|
|
72
|
+
* @returns equivalent duration in milliseconds
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* minutesToMs(1); // 60000
|
|
77
|
+
* minutesToMs(5); // 300000
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
21
80
|
export declare function minutesToMs(minutes?: number): Minutes;
|
|
81
|
+
/**
|
|
82
|
+
* Converts days to minutes.
|
|
83
|
+
*
|
|
84
|
+
* @param days - number of days (defaults to 1)
|
|
85
|
+
* @returns equivalent duration in minutes
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* daysToMinutes(1); // 1440
|
|
90
|
+
* daysToMinutes(7); // 10080
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
22
93
|
export declare function daysToMinutes(days?: number): Minutes;
|
|
94
|
+
/**
|
|
95
|
+
* Returns the {@link MAX_FUTURE_DATE} sentinel value (January 1, 9999 UTC).
|
|
96
|
+
*
|
|
97
|
+
* Useful as a default for "no expiration" comparisons.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* const futureDate = maxFutureDate();
|
|
102
|
+
* // futureDate === MAX_FUTURE_DATE
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
23
105
|
export declare function maxFutureDate(): Date;
|
|
106
|
+
/**
|
|
107
|
+
* Checks whether the given date matches the {@link MAX_FUTURE_DATE} sentinel.
|
|
108
|
+
*
|
|
109
|
+
* @param date - date to check
|
|
110
|
+
* @returns whether the date is the max future sentinel
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```ts
|
|
114
|
+
* isMaxFutureDate(MAX_FUTURE_DATE); // true
|
|
115
|
+
* isMaxFutureDate(new Date()); // false
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
24
118
|
export declare function isMaxFutureDate(date: Date): boolean;
|
|
119
|
+
/**
|
|
120
|
+
* Returns the start of the current minute for the given time, effectively truncating seconds and milliseconds.
|
|
121
|
+
*
|
|
122
|
+
* Defaults to the current time if no input is provided.
|
|
123
|
+
*
|
|
124
|
+
* @param time - date to truncate (defaults to now)
|
|
125
|
+
* @returns date rounded down to the start of the minute
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```ts
|
|
129
|
+
* const date = new Date('2024-01-01T12:30:45.123Z');
|
|
130
|
+
* const result = latestMinute(date);
|
|
131
|
+
* // result === 2024-01-01T12:30:00.000Z
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
25
134
|
export declare function latestMinute(time?: Date): Date;
|
|
135
|
+
/**
|
|
136
|
+
* Converts the input to an ISO 8601 date string.
|
|
137
|
+
*
|
|
138
|
+
* @param input - date or date string to convert
|
|
139
|
+
* @returns the ISO 8601 string representation
|
|
140
|
+
* @throws {Error} When the input cannot be parsed as a valid date.
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```ts
|
|
144
|
+
* toISODateString(new Date('2024-01-01T00:00:00.000Z'));
|
|
145
|
+
* // '2024-01-01T00:00:00.000Z'
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
26
148
|
export declare function toISODateString(input: DateOrDateString): ISO8601DateString;
|
|
27
149
|
/**
|
|
28
|
-
* Guesses the current system's timezone.
|
|
150
|
+
* Guesses the current system's timezone using the Intl API.
|
|
151
|
+
*
|
|
152
|
+
* @returns the IANA timezone string (e.g. "America/Chicago"), or undefined if detection fails
|
|
29
153
|
*
|
|
30
|
-
* @
|
|
154
|
+
* @example
|
|
155
|
+
* ```ts
|
|
156
|
+
* const tz = guessCurrentTimezone();
|
|
157
|
+
* // tz === 'America/New_York' (or similar)
|
|
158
|
+
* ```
|
|
31
159
|
*/
|
|
32
160
|
export declare function guessCurrentTimezone(): TimezoneString | undefined;
|
|
33
161
|
/**
|
|
34
|
-
*
|
|
162
|
+
* Returns the current system's timezone, throwing if detection fails.
|
|
35
163
|
*
|
|
36
|
-
* @
|
|
164
|
+
* Convenience wrapper around {@link guessCurrentTimezone} for contexts where a timezone is required.
|
|
165
|
+
*
|
|
166
|
+
* @returns the IANA timezone string
|
|
167
|
+
* @throws {Error} When the timezone cannot be detected from the Intl API.
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```ts
|
|
171
|
+
* const tz = requireCurrentTimezone();
|
|
172
|
+
* // tz === 'America/New_York' (guaranteed non-undefined)
|
|
173
|
+
* ```
|
|
37
174
|
*/
|
|
38
175
|
export declare function requireCurrentTimezone(): TimezoneString;
|
|
176
|
+
/**
|
|
177
|
+
* Null-safe variant of {@link toJsDate} that returns undefined for null/undefined input.
|
|
178
|
+
*
|
|
179
|
+
* @param input - date, date string, or null/undefined
|
|
180
|
+
* @returns the parsed Date or undefined if input was nullish
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```ts
|
|
184
|
+
* safeToJsDate('2024-01-01T00:00:00.000Z'); // Date instance
|
|
185
|
+
* safeToJsDate(undefined); // undefined
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
39
188
|
export declare function safeToJsDate(input: Maybe<DateOrDateString | UTCDateString>): Maybe<Date>;
|
|
40
189
|
/**
|
|
41
|
-
* Converts the input
|
|
190
|
+
* Converts the input to a JavaScript Date instance. Accepts Date objects, ISO 8601 strings, UTC date strings, and unix millisecond timestamps.
|
|
191
|
+
*
|
|
192
|
+
* @param input - value to convert
|
|
193
|
+
* @returns the parsed Date
|
|
42
194
|
*
|
|
43
|
-
* @
|
|
44
|
-
*
|
|
195
|
+
* @example
|
|
196
|
+
* ```ts
|
|
197
|
+
* toJsDate('2024-01-01T00:00:00.000Z'); // Date instance
|
|
198
|
+
* toJsDate(new Date()); // same Date returned
|
|
199
|
+
* toJsDate(1704067200000); // Date from unix ms
|
|
200
|
+
* ```
|
|
45
201
|
*/
|
|
46
202
|
export declare function toJsDate(input: DateOrDateString | UTCDateString | UnixDateTimeMillisecondsNumber): Date;
|
|
203
|
+
/**
|
|
204
|
+
* Parses a date string or unix milliseconds timestamp into a Date, returning undefined if the result is invalid.
|
|
205
|
+
*
|
|
206
|
+
* Prefers `parseISO` for ISO 8601 strings to avoid browser inconsistencies with `new Date()`.
|
|
207
|
+
*
|
|
208
|
+
* @param input - ISO 8601 string, UTC date string, or unix milliseconds
|
|
209
|
+
* @returns the parsed Date, or undefined if invalid
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```ts
|
|
213
|
+
* parseJsDateString('2020-04-30T00:00:00.000'); // Date instance
|
|
214
|
+
* parseJsDateString('Sat, 03 Feb 2001 04:05:06 GMT'); // Date instance
|
|
215
|
+
* parseJsDateString('not-a-date'); // undefined
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
47
218
|
export declare function parseJsDateString(input: ISO8601DateString | UTCDateString | UnixDateTimeMillisecondsNumber): Maybe<Date>;
|
|
48
219
|
/**
|
|
49
|
-
* Returns the earliest date from the input array.
|
|
220
|
+
* Returns the earliest (minimum) date from the input array, ignoring null/undefined entries.
|
|
221
|
+
*
|
|
222
|
+
* @param dates - array of dates that may contain null/undefined
|
|
223
|
+
* @param defaultDate - fallback if no valid dates exist
|
|
224
|
+
* @returns the earliest date, or the default if the array has no valid dates
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* ```ts
|
|
228
|
+
* const a = new Date('2024-01-01');
|
|
229
|
+
* const b = new Date('2024-06-01');
|
|
230
|
+
* earliestDate([a, b]); // a
|
|
231
|
+
* earliestDate([null, undefined], new Date()); // falls back to default
|
|
232
|
+
* ```
|
|
50
233
|
*/
|
|
51
234
|
export declare function earliestDate(dates: Maybe<Date>[]): Maybe<Date>;
|
|
52
235
|
export declare function earliestDate(dates: Maybe<Date>[], defaultDate: Date): Date;
|
|
53
236
|
/**
|
|
54
|
-
* Returns the latest date from the input array.
|
|
237
|
+
* Returns the latest (maximum) date from the input array, ignoring null/undefined entries.
|
|
238
|
+
*
|
|
239
|
+
* @param dates - array of dates that may contain null/undefined
|
|
240
|
+
* @param defaultDate - fallback if no valid dates exist
|
|
241
|
+
* @returns the latest date, or the default if the array has no valid dates
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* ```ts
|
|
245
|
+
* const a = new Date('2024-01-01');
|
|
246
|
+
* const b = new Date('2024-06-01');
|
|
247
|
+
* latestDate([a, b]); // b
|
|
248
|
+
* latestDate([null], new Date()); // falls back to default
|
|
249
|
+
* ```
|
|
55
250
|
*/
|
|
56
251
|
export declare function latestDate(dates: Maybe<Date>[]): Maybe<Date>;
|
|
57
252
|
export declare function latestDate(dates: Maybe<Date>[], defaultDate: Date): Date;
|
|
58
253
|
/**
|
|
59
|
-
*
|
|
254
|
+
* Null-safe date comparison that returns true when `a` is chronologically after `b`.
|
|
255
|
+
*
|
|
256
|
+
* If either date is null/undefined, returns the default value instead.
|
|
257
|
+
*
|
|
258
|
+
* @param a - first date
|
|
259
|
+
* @param b - second date to compare against
|
|
260
|
+
* @param defaultValue - returned when either date is nullish
|
|
261
|
+
* @returns whether `a` is after `b`, or the default
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* ```ts
|
|
265
|
+
* const jan = new Date('2024-01-01');
|
|
266
|
+
* const feb = new Date('2024-02-01');
|
|
267
|
+
* isAfter(feb, jan); // true
|
|
268
|
+
* isAfter(null, jan); // undefined
|
|
269
|
+
* isAfter(null, jan, false); // false
|
|
270
|
+
* ```
|
|
60
271
|
*/
|
|
61
272
|
export declare function isAfter(a: Maybe<Date>, b: Maybe<Date>): Maybe<boolean>;
|
|
62
273
|
export declare function isAfter(a: Maybe<Date>, b: Maybe<Date>, defaultValue: boolean): boolean;
|
|
63
274
|
/**
|
|
64
|
-
*
|
|
275
|
+
* Null-safe date comparison that returns true when `a` is chronologically before `b`.
|
|
276
|
+
*
|
|
277
|
+
* If either date is null/undefined, returns the default value instead.
|
|
278
|
+
*
|
|
279
|
+
* @param a - first date
|
|
280
|
+
* @param b - second date to compare against
|
|
281
|
+
* @param defaultValue - returned when either date is nullish
|
|
282
|
+
* @returns whether `a` is before `b`, or the default
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* ```ts
|
|
286
|
+
* const jan = new Date('2024-01-01');
|
|
287
|
+
* const feb = new Date('2024-02-01');
|
|
288
|
+
* isBefore(jan, feb); // true
|
|
289
|
+
* isBefore(null, feb); // undefined
|
|
290
|
+
* isBefore(null, feb, false); // false
|
|
291
|
+
* ```
|
|
65
292
|
*/
|
|
66
293
|
export declare function isBefore(a: Maybe<Date>, b: Maybe<Date>): Maybe<boolean>;
|
|
67
294
|
export declare function isBefore(a: Maybe<Date>, b: Maybe<Date>, defaultValue: boolean): boolean;
|
|
68
295
|
/**
|
|
69
|
-
*
|
|
296
|
+
* Null-safe exact date equality check (millisecond precision).
|
|
70
297
|
*
|
|
71
|
-
*
|
|
298
|
+
* When both dates are null/undefined and no default is provided, returns true (treating two absent values as equal).
|
|
72
299
|
*
|
|
73
|
-
* @param a
|
|
74
|
-
* @param b
|
|
300
|
+
* @param a - first date
|
|
301
|
+
* @param b - second date
|
|
302
|
+
* @param defaultValue - returned when either date is nullish (defaults to `a == b` if not provided)
|
|
303
|
+
* @returns whether the dates are exactly equal
|
|
304
|
+
*
|
|
305
|
+
* @example
|
|
306
|
+
* ```ts
|
|
307
|
+
* const d = new Date('2024-01-01T00:00:00.000Z');
|
|
308
|
+
* isSameDate(d, new Date(d.getTime())); // true
|
|
309
|
+
* isSameDate(null, null); // true
|
|
310
|
+
* isSameDate(null, d); // false
|
|
311
|
+
* ```
|
|
75
312
|
*/
|
|
76
313
|
export declare function isSameDate(a: Maybe<Date>, b: Maybe<Date>): boolean;
|
|
77
314
|
export declare function isSameDate(a: Maybe<Date>, b: Maybe<Date>, defaultValue: boolean): boolean;
|
|
78
315
|
export declare function isSameDate(a: Maybe<Date>, b: Maybe<Date>, defaultValue: Maybe<boolean>): Maybe<boolean>;
|
|
79
316
|
/**
|
|
80
|
-
*
|
|
317
|
+
* Null-safe date equality check that compares only down to the minute (ignoring seconds and milliseconds).
|
|
318
|
+
*
|
|
319
|
+
* Rounds both dates down to the start of their respective minutes before comparing.
|
|
81
320
|
*
|
|
82
|
-
* @param a
|
|
83
|
-
* @param b
|
|
321
|
+
* @param a - first date
|
|
322
|
+
* @param b - second date
|
|
323
|
+
* @param defaultValue - returned when either date is nullish
|
|
324
|
+
* @returns whether both dates fall within the same minute
|
|
325
|
+
*
|
|
326
|
+
* @example
|
|
327
|
+
* ```ts
|
|
328
|
+
* const a = new Date('2024-01-01T12:30:00.000Z');
|
|
329
|
+
* const b = new Date('2024-01-01T12:30:45.999Z');
|
|
330
|
+
* isSameDateHoursAndMinutes(a, b); // true
|
|
331
|
+
* ```
|
|
84
332
|
*/
|
|
85
333
|
export declare function isSameDateHoursAndMinutes(a: Maybe<Date>, b: Maybe<Date>): boolean;
|
|
86
334
|
export declare function isSameDateHoursAndMinutes(a: Maybe<Date>, b: Maybe<Date>, defaultValue: boolean): boolean;
|
|
87
335
|
export declare function isSameDateHoursAndMinutes(a: Maybe<Date>, b: Maybe<Date>, defaultValue: Maybe<boolean>): Maybe<boolean>;
|
|
88
336
|
/**
|
|
89
|
-
*
|
|
337
|
+
* Null-safe day-level date equality check (same year, month, and day, ignoring time).
|
|
338
|
+
*
|
|
339
|
+
* When both dates are null/undefined and no default is provided, returns true (treating two absent values as equal).
|
|
90
340
|
*
|
|
91
|
-
*
|
|
341
|
+
* @param a - first date
|
|
342
|
+
* @param b - second date
|
|
343
|
+
* @param defaultValue - returned when either date is nullish
|
|
344
|
+
* @returns whether both dates fall on the same calendar day
|
|
92
345
|
*
|
|
93
|
-
* @
|
|
94
|
-
*
|
|
346
|
+
* @example
|
|
347
|
+
* ```ts
|
|
348
|
+
* const morning = new Date('2024-01-01T08:00:00.000Z');
|
|
349
|
+
* const evening = new Date('2024-01-01T20:00:00.000Z');
|
|
350
|
+
* isSameDateDay(morning, evening); // true
|
|
351
|
+
* isSameDateDay(null, null); // true
|
|
352
|
+
* ```
|
|
95
353
|
*/
|
|
96
354
|
export declare function isSameDateDay(a: Maybe<Date>, b: Maybe<Date>): boolean;
|
|
97
355
|
export declare function isSameDateDay(a: Maybe<Date>, b: Maybe<Date>, defaultValue: boolean): boolean;
|
|
98
356
|
export declare function isSameDateDay(a: Maybe<Date>, b: Maybe<Date>, defaultValue: Maybe<boolean>): Maybe<boolean>;
|
|
99
357
|
/**
|
|
100
|
-
* Converts
|
|
358
|
+
* Converts a local date to a UTC date representing the same calendar day at midnight.
|
|
359
|
+
*
|
|
360
|
+
* Useful for normalizing dates to UTC day boundaries when comparing calendar days across timezones.
|
|
101
361
|
*
|
|
102
|
-
*
|
|
362
|
+
* @param date - local date to convert
|
|
363
|
+
* @returns a UTC Date at midnight for the same calendar day
|
|
364
|
+
*
|
|
365
|
+
* @example
|
|
366
|
+
* ```ts
|
|
367
|
+
* const local = new Date(2021, 0, 1, 15, 30); // Jan 1, 2021 3:30 PM local
|
|
368
|
+
* const utcDay = utcDayForDate(local);
|
|
369
|
+
* // utcDay === 2021-01-01T00:00:00.000Z
|
|
370
|
+
* ```
|
|
103
371
|
*/
|
|
104
372
|
export declare function utcDayForDate(date: Date): Date;
|
|
105
373
|
/**
|
|
106
|
-
* Creates a new
|
|
374
|
+
* Creates a new UTC date by copying the UTC hours and minutes from `fromDate` onto the `target` date's UTC year/month/day.
|
|
375
|
+
*
|
|
376
|
+
* Optionally zeroes out seconds and milliseconds when `roundDownToMinute` is true.
|
|
377
|
+
*
|
|
378
|
+
* @param target - date providing the UTC year/month/day
|
|
379
|
+
* @param fromDate - date providing the UTC hours/minutes
|
|
380
|
+
* @param roundDownToMinute - whether to zero out seconds and milliseconds
|
|
381
|
+
* @returns a new UTC Date combining the target's day with the source's time
|
|
382
|
+
*
|
|
383
|
+
* @example
|
|
384
|
+
* ```ts
|
|
385
|
+
* const target = new Date('2024-03-15T00:00:00.000Z');
|
|
386
|
+
* const source = new Date('2024-01-01T14:30:45.000Z');
|
|
387
|
+
* const result = copyHoursAndMinutesFromUTCDate(target, source, true);
|
|
388
|
+
* // result === 2024-03-15T14:30:00.000Z
|
|
389
|
+
* ```
|
|
107
390
|
*/
|
|
108
391
|
export declare function copyHoursAndMinutesFromUTCDate(target: Date, fromDate: Date, roundDownToMinute?: boolean): Date;
|
|
109
392
|
/**
|
|
110
|
-
*
|
|
393
|
+
* Sets the hours and optionally minutes on a target date (defaults to now), with optional rounding to strip seconds/milliseconds.
|
|
111
394
|
*
|
|
112
|
-
*
|
|
395
|
+
* @param config - hours, optional minutes, and rounding options
|
|
396
|
+
* @param target - date to modify (defaults to the current date/time)
|
|
397
|
+
* @returns a new Date with the specified time values applied
|
|
398
|
+
*
|
|
399
|
+
* @example
|
|
400
|
+
* ```ts
|
|
401
|
+
* const target = new Date('2024-01-01T00:00:00.000Z');
|
|
402
|
+
* const result = copyHoursAndMinutesToDate({ hours: 14, minutes: 30 }, target);
|
|
403
|
+
* // result has hours=14, minutes=30, seconds=0, milliseconds=0
|
|
404
|
+
* ```
|
|
113
405
|
*/
|
|
114
406
|
export declare function copyHoursAndMinutesToDate({ hours, minutes, removeSeconds, roundDownToMinute }: {
|
|
115
407
|
hours: number;
|
|
@@ -117,65 +409,200 @@ export declare function copyHoursAndMinutesToDate({ hours, minutes, removeSecond
|
|
|
117
409
|
removeSeconds?: boolean;
|
|
118
410
|
roundDownToMinute?: boolean;
|
|
119
411
|
}, target?: Maybe<Date>): Date;
|
|
412
|
+
/**
|
|
413
|
+
* Alias for {@link copyHoursAndMinutesToDate}.
|
|
414
|
+
*/
|
|
120
415
|
export declare const copyHoursAndMinutesToToday: typeof copyHoursAndMinutesToDate;
|
|
121
416
|
/**
|
|
122
|
-
*
|
|
417
|
+
* Truncates seconds and milliseconds from the input date, effectively rounding down to the start of the minute.
|
|
418
|
+
*
|
|
419
|
+
* @param date - date to round (defaults to now)
|
|
420
|
+
* @returns the date with seconds and milliseconds set to zero
|
|
421
|
+
*
|
|
422
|
+
* @example
|
|
423
|
+
* ```ts
|
|
424
|
+
* const date = new Date('2024-01-01T12:30:45.123Z');
|
|
425
|
+
* roundDownToMinute(date); // 2024-01-01T12:30:00.000Z
|
|
426
|
+
* ```
|
|
123
427
|
*/
|
|
124
428
|
export declare function roundDownToMinute(date?: Date): Date;
|
|
125
429
|
/**
|
|
126
|
-
*
|
|
430
|
+
* Truncates minutes, seconds, and milliseconds from the input date, effectively rounding down to the start of the hour.
|
|
431
|
+
*
|
|
432
|
+
* @param date - date to round (defaults to now)
|
|
433
|
+
* @returns the date with minutes, seconds, and milliseconds set to zero
|
|
434
|
+
*
|
|
435
|
+
* @example
|
|
436
|
+
* ```ts
|
|
437
|
+
* const date = new Date('2024-01-01T12:30:45.123Z');
|
|
438
|
+
* roundDownToHour(date); // 2024-01-01T12:00:00.000Z
|
|
439
|
+
* ```
|
|
127
440
|
*/
|
|
128
441
|
export declare function roundDownToHour(date?: Date): Date;
|
|
442
|
+
/**
|
|
443
|
+
* Convenience function that rounds a date down (floor) to the specified unit.
|
|
444
|
+
*
|
|
445
|
+
* @param date - date to round
|
|
446
|
+
* @param roundToUnit - time unit to round to ('hour', 'minute', or 'second')
|
|
447
|
+
* @returns a new Date rounded down to the unit boundary
|
|
448
|
+
*/
|
|
129
449
|
export declare function roundDateDownTo(date: Date, roundToUnit: DateHourMinuteOrSecond): Date;
|
|
450
|
+
/**
|
|
451
|
+
* Rounds a date or unix timestamp to the nearest unit boundary using the specified rounding direction.
|
|
452
|
+
*
|
|
453
|
+
* Preserves the input type: Date inputs return Date, number inputs return number.
|
|
454
|
+
*
|
|
455
|
+
* @param date - date or unix millisecond timestamp to round
|
|
456
|
+
* @param roundToUnit - time unit to round to ('hour', 'minute', or 'second')
|
|
457
|
+
* @param roundType - rounding direction ('floor' or 'ceil', defaults to 'floor')
|
|
458
|
+
* @returns the rounded value in the same type as the input
|
|
459
|
+
*/
|
|
130
460
|
export declare function roundDateTo(date: Date, roundToUnit: DateHourMinuteOrSecond, roundType?: FloorOrCeilRounding): Date;
|
|
131
461
|
export declare function roundDateTo(unixDateTimeNumber: UnixDateTimeMillisecondsNumber, roundToUnit: DateHourMinuteOrSecond, roundType?: FloorOrCeilRounding): UnixDateTimeMillisecondsNumber;
|
|
462
|
+
/**
|
|
463
|
+
* Rounds a date or unix timestamp to the nearest unit boundary, always returning a Date.
|
|
464
|
+
*
|
|
465
|
+
* @param date - date or unix millisecond timestamp to round
|
|
466
|
+
* @param roundToUnit - time unit to round to ('hour', 'minute', or 'second')
|
|
467
|
+
* @param roundType - rounding direction ('floor' or 'ceil', defaults to 'floor')
|
|
468
|
+
* @returns a new Date rounded to the unit boundary
|
|
469
|
+
*
|
|
470
|
+
* @example
|
|
471
|
+
* ```ts
|
|
472
|
+
* const date = new Date('2024-01-01T01:05:07.123Z');
|
|
473
|
+
* roundDateToDate(date, 'hour', 'floor'); // 2024-01-01T01:00:00.000Z
|
|
474
|
+
* roundDateToDate(date, 'minute', 'ceil'); // 2024-01-01T01:06:00.000Z
|
|
475
|
+
* ```
|
|
476
|
+
*/
|
|
132
477
|
export declare function roundDateToDate(date: DateOrUnixDateTimeMillisecondsNumber, roundToUnit: DateHourMinuteOrSecond, roundType?: FloorOrCeilRounding): Date;
|
|
133
478
|
/**
|
|
134
|
-
* Rounds
|
|
479
|
+
* Rounds a date or unix timestamp to the nearest hour, minute, or second boundary using pure arithmetic on the millisecond value.
|
|
480
|
+
*
|
|
481
|
+
* This approach avoids DST issues that can occur with date-fns `set()` or native `Date.setMinutes()`, which may produce
|
|
482
|
+
* incorrect results during fall-back transitions.
|
|
135
483
|
*
|
|
136
|
-
* @param date
|
|
137
|
-
* @param roundToUnit
|
|
138
|
-
* @param roundType
|
|
139
|
-
* @returns
|
|
484
|
+
* @param date - date or unix millisecond timestamp to round
|
|
485
|
+
* @param roundToUnit - time unit to round to ('hour', 'minute', or 'second')
|
|
486
|
+
* @param roundType - rounding direction ('floor' truncates, 'ceil' rounds up)
|
|
487
|
+
* @returns the rounded unix millisecond timestamp
|
|
488
|
+
*
|
|
489
|
+
* @example
|
|
490
|
+
* ```ts
|
|
491
|
+
* const date = new Date('2024-01-01T01:05:07.123Z');
|
|
492
|
+
* roundDateToUnixDateTimeNumber(date, 'hour', 'floor'); // ms for 2024-01-01T01:00:00.000Z
|
|
493
|
+
* roundDateToUnixDateTimeNumber(date, 'minute', 'ceil'); // ms for 2024-01-01T01:06:00.000Z
|
|
494
|
+
* ```
|
|
140
495
|
*/
|
|
141
496
|
export declare function roundDateToUnixDateTimeNumber(date: DateOrUnixDateTimeMillisecondsNumber, roundToUnit: DateHourMinuteOrSecond, roundType?: FloorOrCeilRounding): UnixDateTimeMillisecondsNumber;
|
|
497
|
+
/**
|
|
498
|
+
* Function that reduces an array (or single value) of possibly-null dates into a single Date result.
|
|
499
|
+
*/
|
|
142
500
|
export type ReduceDatesFunction = (inputDates: ArrayOrValue<Maybe<Date>>) => Maybe<Date>;
|
|
501
|
+
/**
|
|
502
|
+
* Creates a {@link ReduceDatesFunction} that filters out null/undefined values, then applies the given reducer to the remaining dates.
|
|
503
|
+
*
|
|
504
|
+
* Returns undefined if no valid dates are present in the input.
|
|
505
|
+
*
|
|
506
|
+
* @param reduceDates - reducer function applied to the non-null dates array
|
|
507
|
+
* @returns a function that accepts an array or single date value and returns the reduced result
|
|
508
|
+
*
|
|
509
|
+
* @example
|
|
510
|
+
* ```ts
|
|
511
|
+
* import { min } from 'date-fns';
|
|
512
|
+
* const findMin = reduceDatesFunction(min);
|
|
513
|
+
* findMin([new Date('2024-01-01'), new Date('2024-06-01')]); // Jan 1
|
|
514
|
+
* findMin([null, undefined]); // undefined
|
|
515
|
+
* ```
|
|
516
|
+
*/
|
|
143
517
|
export declare function reduceDatesFunction(reduceDates: (dates: Date[]) => Maybe<Date>): ReduceDatesFunction;
|
|
144
518
|
/**
|
|
145
|
-
* Finds the minimum date
|
|
519
|
+
* Finds the minimum (earliest) date from the input, ignoring null/undefined values. Returns undefined if no valid dates are provided.
|
|
520
|
+
*
|
|
521
|
+
* @example
|
|
522
|
+
* ```ts
|
|
523
|
+
* findMinDate([new Date('2024-06-01'), new Date('2024-01-01')]); // Jan 1
|
|
524
|
+
* findMinDate([null]); // undefined
|
|
525
|
+
* ```
|
|
146
526
|
*/
|
|
147
527
|
export declare const findMinDate: ReduceDatesFunction;
|
|
148
528
|
/**
|
|
149
|
-
* Finds the maximum date
|
|
529
|
+
* Finds the maximum (latest) date from the input, ignoring null/undefined values. Returns undefined if no valid dates are provided.
|
|
530
|
+
*
|
|
531
|
+
* @example
|
|
532
|
+
* ```ts
|
|
533
|
+
* findMaxDate([new Date('2024-01-01'), new Date('2024-06-01')]); // Jun 1
|
|
534
|
+
* findMaxDate([null]); // undefined
|
|
535
|
+
* ```
|
|
150
536
|
*/
|
|
151
537
|
export declare const findMaxDate: ReduceDatesFunction;
|
|
152
538
|
/**
|
|
153
|
-
*
|
|
539
|
+
* Collects the unique days of the week present among the given values, short-circuiting once all 7 days are found.
|
|
540
|
+
*
|
|
541
|
+
* @param values - items to extract dates from
|
|
542
|
+
* @param readDate - function to extract a Date from each value
|
|
543
|
+
* @returns a Set of DayOfWeek values (0=Sunday through 6=Saturday)
|
|
154
544
|
*
|
|
155
|
-
* @
|
|
156
|
-
*
|
|
545
|
+
* @example
|
|
546
|
+
* ```ts
|
|
547
|
+
* const dates = [new Date('2024-01-01'), new Date('2024-01-02')]; // Mon, Tue
|
|
548
|
+
* const days = readDaysOfWeek(dates, (d) => d);
|
|
549
|
+
* // days contains DayOfWeek.MONDAY and DayOfWeek.TUESDAY
|
|
550
|
+
* ```
|
|
157
551
|
*/
|
|
158
552
|
export declare function readDaysOfWeek<T>(values: T[], readDate: ReadDateFunction<T>): Set<DayOfWeek>;
|
|
553
|
+
/**
|
|
554
|
+
* Returns the display names of the unique days of the week present among the values, sorted by day number (Sunday first).
|
|
555
|
+
*
|
|
556
|
+
* @param values - items to extract dates from
|
|
557
|
+
* @param readDate - function to extract a Date from each value
|
|
558
|
+
* @param nameFunction - function that converts a DayOfWeek to its display name
|
|
559
|
+
* @returns sorted array of day name strings
|
|
560
|
+
*
|
|
561
|
+
* @example
|
|
562
|
+
* ```ts
|
|
563
|
+
* const dates = [new Date('2024-01-02'), new Date('2024-01-01')]; // Tue, Mon
|
|
564
|
+
* const names = readDaysOfWeekNames(dates, (d) => d, (day) => ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'][day]);
|
|
565
|
+
* // names === ['Mon', 'Tue']
|
|
566
|
+
* ```
|
|
567
|
+
*/
|
|
159
568
|
export declare function readDaysOfWeekNames<T>(values: T[], readDate: ReadDateFunction<T>, nameFunction: DayOfWeekNameFunction): string[];
|
|
160
569
|
/**
|
|
161
|
-
*
|
|
570
|
+
* Checks whether the given date falls exactly at midnight (00:00:00.000) in UTC.
|
|
162
571
|
*
|
|
163
|
-
* @param date
|
|
164
|
-
* @returns
|
|
572
|
+
* @param date - date to check
|
|
573
|
+
* @returns whether all UTC time components are zero
|
|
574
|
+
*
|
|
575
|
+
* @example
|
|
576
|
+
* ```ts
|
|
577
|
+
* isStartOfDayInUTC(new Date('2024-01-01T00:00:00.000Z')); // true
|
|
578
|
+
* isStartOfDayInUTC(new Date('2024-01-01T00:00:01.000Z')); // false
|
|
579
|
+
* ```
|
|
165
580
|
*/
|
|
166
581
|
export declare function isStartOfDayInUTC(date: Date): boolean;
|
|
167
582
|
/**
|
|
168
|
-
*
|
|
583
|
+
* Checks whether the given date falls at the end of the day (23:59:59.999) in UTC.
|
|
584
|
+
*
|
|
585
|
+
* @param date - date to check
|
|
586
|
+
* @param minutesOnly - if true, only checks hours and minutes (23:59), ignoring seconds and milliseconds
|
|
587
|
+
* @returns whether the date represents end-of-day in UTC
|
|
169
588
|
*
|
|
170
|
-
* @
|
|
171
|
-
*
|
|
172
|
-
*
|
|
589
|
+
* @example
|
|
590
|
+
* ```ts
|
|
591
|
+
* isEndOfDayInUTC(new Date('2024-01-01T23:59:59.999Z')); // true
|
|
592
|
+
* isEndOfDayInUTC(new Date('2024-01-01T23:59:00.000Z'), true); // true (minutes-only mode)
|
|
593
|
+
* ```
|
|
173
594
|
*/
|
|
174
595
|
export declare function isEndOfDayInUTC(date: Date, minutesOnly?: boolean): boolean;
|
|
175
596
|
/**
|
|
176
|
-
*
|
|
597
|
+
* Checks whether the given date falls exactly at midnight (00:00:00.000) in the system's local timezone.
|
|
598
|
+
*
|
|
599
|
+
* @param date - date to check
|
|
600
|
+
* @returns whether all local time components are zero
|
|
177
601
|
*
|
|
178
|
-
* @
|
|
179
|
-
*
|
|
602
|
+
* @example
|
|
603
|
+
* ```ts
|
|
604
|
+
* const midnight = new Date(2024, 0, 1, 0, 0, 0, 0);
|
|
605
|
+
* isStartOfDayForSystem(midnight); // true
|
|
606
|
+
* ```
|
|
180
607
|
*/
|
|
181
608
|
export declare function isStartOfDayForSystem(date: Date): boolean;
|