@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
|
@@ -2,24 +2,37 @@ import { type DateOrDateString, type DateOrDayString, type ISO8601DateString, ty
|
|
|
2
2
|
import { formatDistance, formatDistanceStrict } from 'date-fns';
|
|
3
3
|
import { type DateRange } from './date.range';
|
|
4
4
|
import { type DateTimezoneUtcNormalFunctionInput } from './date.timezone';
|
|
5
|
+
/**
|
|
6
|
+
* Maps a Date to a formatted string representation.
|
|
7
|
+
*/
|
|
5
8
|
export type FormatDateFunction = MapFunction<Date, string>;
|
|
9
|
+
/**
|
|
10
|
+
* Formats a DateRange into a human-readable string. Only accepts a DateRange input.
|
|
11
|
+
*/
|
|
6
12
|
export type FormatStrictDateRangeFunction = (startOrDateRange: DateRange) => string;
|
|
13
|
+
/**
|
|
14
|
+
* Formats a date range into a human-readable string. Accepts either a DateRange object
|
|
15
|
+
* or separate start/end Date arguments.
|
|
16
|
+
*/
|
|
7
17
|
export type FormatDateRangeFunction = FormatStrictDateRangeFunction & ((startOrDateRange: Date, inputEnd?: Date) => string);
|
|
18
|
+
/**
|
|
19
|
+
* Configuration for creating a {@link FormatDateRangeFunction} via {@link formatDateRangeFunction}.
|
|
20
|
+
*/
|
|
8
21
|
export interface FormatDateRangeFunctionConfig {
|
|
9
22
|
/**
|
|
10
|
-
*
|
|
23
|
+
* Function used to format each individual date in the range.
|
|
11
24
|
*/
|
|
12
25
|
format: FormatDateFunction;
|
|
13
26
|
/**
|
|
14
|
-
* Whether
|
|
27
|
+
* Whether to normalize dates to UTC before formatting.
|
|
15
28
|
*/
|
|
16
29
|
normalizeToUTC?: boolean;
|
|
17
30
|
/**
|
|
18
|
-
*
|
|
31
|
+
* Separator string placed between the formatted start and end dates. Defaults to `'-'`.
|
|
19
32
|
*/
|
|
20
33
|
separator?: string;
|
|
21
34
|
/**
|
|
22
|
-
* Whether
|
|
35
|
+
* Whether to output a single formatted date when both dates fall on the same day.
|
|
23
36
|
*
|
|
24
37
|
* False by default.
|
|
25
38
|
*/
|
|
@@ -27,18 +40,49 @@ export interface FormatDateRangeFunctionConfig {
|
|
|
27
40
|
}
|
|
28
41
|
export type FormatDateRangeFunctionConfigInput = FormatDateFunction | FormatDateRangeFunctionConfig;
|
|
29
42
|
/**
|
|
30
|
-
* Creates a FormatDateRangeFunction
|
|
43
|
+
* Creates a reusable {@link FormatDateRangeFunction} from the given config or format function.
|
|
44
|
+
*
|
|
45
|
+
* Useful when the same range formatting logic needs to be applied repeatedly with consistent settings.
|
|
46
|
+
*
|
|
47
|
+
* @param inputConfig - format function or full configuration
|
|
31
48
|
*
|
|
32
|
-
* @
|
|
33
|
-
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* import { formatDateRangeFunction, formatToTimeString } from '@dereekb/date';
|
|
52
|
+
*
|
|
53
|
+
* const formatRange = formatDateRangeFunction({
|
|
54
|
+
* format: formatToTimeString,
|
|
55
|
+
* separator: 'to',
|
|
56
|
+
* simplifySameDate: true
|
|
57
|
+
* });
|
|
58
|
+
*
|
|
59
|
+
* const result = formatRange({ start: new Date('2024-01-15T09:00:00'), end: new Date('2024-01-15T17:00:00') });
|
|
60
|
+
* // "9:00 AM to 5:00 PM"
|
|
61
|
+
* ```
|
|
34
62
|
*/
|
|
35
63
|
export declare function formatDateRangeFunction(inputConfig: FormatDateRangeFunctionConfigInput): FormatDateRangeFunction;
|
|
36
64
|
/**
|
|
37
|
-
*
|
|
65
|
+
* Convenience function that formats a date range in a single call without creating a reusable function.
|
|
66
|
+
*
|
|
67
|
+
* @param range - date range to format
|
|
68
|
+
* @param inputConfig - format function or full configuration
|
|
69
|
+
* @param separator - optional separator override when inputConfig is a function
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* import { formatDateRange, formatToTimeString } from '@dereekb/date';
|
|
74
|
+
*
|
|
75
|
+
* const result = formatDateRange(
|
|
76
|
+
* { start: new Date('2024-01-15T09:00:00'), end: new Date('2024-01-15T17:00:00') },
|
|
77
|
+
* formatToTimeString
|
|
78
|
+
* );
|
|
79
|
+
* // "9:00 AM - 5:00 PM"
|
|
80
|
+
* ```
|
|
38
81
|
*/
|
|
39
82
|
export declare function formatDateRange(range: DateRange, inputConfig: FormatDateRangeFunctionConfigInput, separator?: string): string;
|
|
40
83
|
/**
|
|
41
|
-
* formatDateRangeDistanceFunction
|
|
84
|
+
* Configuration for {@link formatDateRangeDistanceFunction}. Extends date-fns `formatDistance`/`formatDistanceStrict` options
|
|
85
|
+
* to control how the human-readable distance string is produced.
|
|
42
86
|
*/
|
|
43
87
|
export type FormatDateRangeDistanceFunctionConfig = {
|
|
44
88
|
/**
|
|
@@ -65,93 +109,368 @@ export type FormatDateRangeDistanceFunctionConfig = {
|
|
|
65
109
|
strict: true;
|
|
66
110
|
}));
|
|
67
111
|
/**
|
|
68
|
-
*
|
|
112
|
+
* Creates a reusable function that computes the human-readable distance between the start and end of a date range
|
|
113
|
+
* (e.g., "3 hours", "about 2 days"). Delegates to date-fns `formatDistance` or `formatDistanceStrict`.
|
|
114
|
+
*
|
|
115
|
+
* @param inputConfig - controls distance formatting, optional transforms, and same-day handling
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```ts
|
|
119
|
+
* import { formatDateRangeDistanceFunction } from '@dereekb/date';
|
|
120
|
+
*
|
|
121
|
+
* const formatDist = formatDateRangeDistanceFunction({ strict: true, unit: 'hour' });
|
|
122
|
+
* const result = formatDist({ start: new Date('2024-01-15T09:00:00'), end: new Date('2024-01-15T17:00:00') });
|
|
123
|
+
* // "8 hours"
|
|
124
|
+
* ```
|
|
69
125
|
*/
|
|
70
126
|
export declare function formatDateRangeDistanceFunction(inputConfig: FormatDateRangeDistanceFunctionConfig): FormatDateRangeFunction;
|
|
71
127
|
/**
|
|
72
|
-
*
|
|
128
|
+
* Pre-configured {@link FormatDateRangeFunction} that computes the distance between the start-of-day
|
|
129
|
+
* representations of two dates. Returns `'Today'` or `'Same Day'` when both dates fall on the same calendar day.
|
|
130
|
+
*
|
|
131
|
+
* Primarily retained for backward compatibility; prefer building a custom function via
|
|
132
|
+
* {@link formatDateRangeDistanceFunction} for new use cases.
|
|
73
133
|
*/
|
|
74
134
|
export declare const formatDateDistance: FormatDateRangeFunction;
|
|
75
135
|
/**
|
|
76
|
-
*
|
|
136
|
+
* Convenience function that computes the human-readable distance for a date range in a single call.
|
|
77
137
|
*
|
|
78
|
-
* @param range
|
|
79
|
-
* @param inputConfig
|
|
80
|
-
*
|
|
138
|
+
* @param range - date range to compute distance for
|
|
139
|
+
* @param inputConfig - optional distance formatting configuration
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```ts
|
|
143
|
+
* import { formatDateRangeDistance } from '@dereekb/date';
|
|
144
|
+
*
|
|
145
|
+
* const result = formatDateRangeDistance(
|
|
146
|
+
* { start: new Date('2024-01-15T09:00:00'), end: new Date('2024-01-17T09:00:00') },
|
|
147
|
+
* { addSuffix: true }
|
|
148
|
+
* );
|
|
149
|
+
* // "in 2 days"
|
|
150
|
+
* ```
|
|
81
151
|
*/
|
|
82
152
|
export declare function formatDateRangeDistance(range: DateRange, inputConfig?: FormatDateRangeDistanceFunctionConfig): string;
|
|
83
153
|
/**
|
|
84
|
-
* Formats
|
|
154
|
+
* Formats a date range as a time-only range string when both dates are on the same day,
|
|
155
|
+
* or falls back to a full date+time range when they span multiple days.
|
|
156
|
+
*
|
|
157
|
+
* Same day: `"12:00 AM - 4:00 PM"`
|
|
158
|
+
* Different days: `"01/01/2001 12:00 AM - 01/02/2001 4:00 PM"`
|
|
85
159
|
*
|
|
86
|
-
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```ts
|
|
162
|
+
* import { formatToTimeRangeString } from '@dereekb/date';
|
|
87
163
|
*
|
|
88
|
-
*
|
|
164
|
+
* // Same day - time only
|
|
165
|
+
* formatToTimeRangeString({ start: new Date('2024-01-15T09:00:00'), end: new Date('2024-01-15T17:00:00') });
|
|
166
|
+
* // "9:00 AM - 5:00 PM"
|
|
89
167
|
*
|
|
90
|
-
*
|
|
168
|
+
* // Different days - includes date
|
|
169
|
+
* formatToTimeRangeString({ start: new Date('2024-01-15T09:00:00'), end: new Date('2024-01-16T17:00:00') });
|
|
170
|
+
* // "01/15/2024 9:00 AM - 01/16/2024 5:00 PM"
|
|
171
|
+
* ```
|
|
91
172
|
*/
|
|
92
173
|
export declare function formatToTimeRangeString(startOrDateRange: DateRange): string;
|
|
93
174
|
export declare function formatToTimeRangeString(start: Date, end?: Maybe<Date>): string;
|
|
94
175
|
export declare function formatToTimeRangeString(startOrDateRange: DateRange | Date, end?: Maybe<Date>, onlyTimeRange?: boolean): string;
|
|
95
176
|
/**
|
|
96
|
-
* Formats
|
|
177
|
+
* Formats a date range as a full date+time range string, always including the date portion
|
|
178
|
+
* regardless of whether both dates fall on the same day.
|
|
97
179
|
*
|
|
98
|
-
*
|
|
180
|
+
* Output: `"01/01/2001 12:00 AM - 01/02/2001 4:00 PM"`
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```ts
|
|
184
|
+
* import { formatToDayTimeRangeString } from '@dereekb/date';
|
|
185
|
+
*
|
|
186
|
+
* formatToDayTimeRangeString({ start: new Date('2024-01-15T09:00:00'), end: new Date('2024-01-16T17:00:00') });
|
|
187
|
+
* // "01/15/2024 9:00 AM - 01/16/2024 5:00 PM"
|
|
188
|
+
* ```
|
|
99
189
|
*/
|
|
100
190
|
export declare function formatToDayTimeRangeString(startOrDateRange: DateRange): string;
|
|
101
191
|
export declare function formatToDayTimeRangeString(start: Date, end?: Maybe<Date>): string;
|
|
102
192
|
/**
|
|
103
|
-
* Formats
|
|
193
|
+
* Formats a date range as a day-only range string using `MM/dd/yyyy` by default. When both dates
|
|
194
|
+
* fall on the same day, only a single date is printed. Accepts an optional custom format function.
|
|
195
|
+
*
|
|
196
|
+
* Output: `"02/01/1992 - 03/01/1992"` or `"02/01/1992"` if same day
|
|
104
197
|
*
|
|
105
|
-
*
|
|
198
|
+
* @example
|
|
199
|
+
* ```ts
|
|
200
|
+
* import { formatToDayRangeString } from '@dereekb/date';
|
|
201
|
+
*
|
|
202
|
+
* formatToDayRangeString({ start: new Date('2024-01-15'), end: new Date('2024-01-20') });
|
|
203
|
+
* // "01/15/2024 - 01/20/2024"
|
|
204
|
+
*
|
|
205
|
+
* formatToDayRangeString({ start: new Date('2024-01-15'), end: new Date('2024-01-15') });
|
|
206
|
+
* // "01/15/2024"
|
|
207
|
+
* ```
|
|
106
208
|
*/
|
|
107
209
|
export declare function formatToDayRangeString(startOrDateRange: DateRange, format?: FormatDateFunction): string;
|
|
108
210
|
export declare function formatToDayRangeString(start: Date, end?: Maybe<Date>, format?: FormatDateFunction): string;
|
|
109
211
|
/**
|
|
110
|
-
* Safely formats the input to an
|
|
212
|
+
* Safely formats the input to an ISO 8601 date string, returning `undefined` for invalid or nullish inputs
|
|
213
|
+
* instead of throwing.
|
|
214
|
+
*
|
|
215
|
+
* @param input - date, date string, or nullish value
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```ts
|
|
219
|
+
* import { safeFormatToISO8601DateString } from '@dereekb/date';
|
|
220
|
+
*
|
|
221
|
+
* safeFormatToISO8601DateString(new Date('2024-01-15T12:00:00Z'));
|
|
222
|
+
* // "2024-01-15T12:00:00.000Z"
|
|
223
|
+
*
|
|
224
|
+
* safeFormatToISO8601DateString(null);
|
|
225
|
+
* // undefined
|
|
111
226
|
*
|
|
112
|
-
*
|
|
113
|
-
*
|
|
227
|
+
* safeFormatToISO8601DateString('invalid');
|
|
228
|
+
* // undefined
|
|
229
|
+
* ```
|
|
114
230
|
*/
|
|
115
231
|
export declare function safeFormatToISO8601DateString(input: Maybe<DateOrDateString | UTCDateString>): ISO8601DateString | undefined;
|
|
232
|
+
/**
|
|
233
|
+
* Formats a Date to a full ISO 8601 date-time string via `Date.toISOString()`. Defaults to the current date/time.
|
|
234
|
+
*
|
|
235
|
+
* @param date - date to format; defaults to `new Date()`
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* ```ts
|
|
239
|
+
* import { formatToISO8601DateString } from '@dereekb/date';
|
|
240
|
+
*
|
|
241
|
+
* formatToISO8601DateString(new Date('2024-01-15T12:00:00Z'));
|
|
242
|
+
* // "2024-01-15T12:00:00.000Z"
|
|
243
|
+
* ```
|
|
244
|
+
*/
|
|
116
245
|
export declare function formatToISO8601DateString(date?: Date): ISO8601DayString;
|
|
117
246
|
/**
|
|
118
|
-
* Converts
|
|
247
|
+
* Converts a Date or day string to an ISO 8601 day string (`yyyy-MM-dd`) using the **system timezone**.
|
|
248
|
+
* If the input is already a string, it is returned as-is.
|
|
249
|
+
*
|
|
250
|
+
* Use {@link toISO8601DayStringForUTC} when the UTC date is needed instead.
|
|
119
251
|
*
|
|
120
|
-
*
|
|
252
|
+
* @param dateOrString - date or existing day string
|
|
121
253
|
*
|
|
122
|
-
* @
|
|
123
|
-
*
|
|
254
|
+
* @example
|
|
255
|
+
* ```ts
|
|
256
|
+
* import { toISO8601DayStringForSystem } from '@dereekb/date';
|
|
257
|
+
*
|
|
258
|
+
* toISO8601DayStringForSystem(new Date('2024-01-15T20:00:00'));
|
|
259
|
+
* // "2024-01-15" (in system timezone)
|
|
260
|
+
*
|
|
261
|
+
* toISO8601DayStringForSystem('2024-01-15');
|
|
262
|
+
* // "2024-01-15" (pass-through)
|
|
263
|
+
* ```
|
|
124
264
|
*/
|
|
125
265
|
export declare function toISO8601DayStringForSystem(dateOrString: DateOrDayString): ISO8601DayString;
|
|
266
|
+
/**
|
|
267
|
+
* Formats a Date to an ISO 8601 day string (`yyyy-MM-dd`) using the **system timezone**.
|
|
268
|
+
* Defaults to the current date.
|
|
269
|
+
*
|
|
270
|
+
* @param date - date to format; defaults to `new Date()`
|
|
271
|
+
*
|
|
272
|
+
* @example
|
|
273
|
+
* ```ts
|
|
274
|
+
* import { formatToISO8601DayStringForSystem } from '@dereekb/date';
|
|
275
|
+
*
|
|
276
|
+
* formatToISO8601DayStringForSystem(new Date('2024-01-15T20:00:00'));
|
|
277
|
+
* // "2024-01-15"
|
|
278
|
+
* ```
|
|
279
|
+
*/
|
|
126
280
|
export declare function formatToISO8601DayStringForSystem(date?: Date): ISO8601DayString;
|
|
127
281
|
/**
|
|
128
|
-
* Converts
|
|
282
|
+
* Converts a Date or day string to an ISO 8601 day string (`yyyy-MM-dd`) using the **UTC date**.
|
|
283
|
+
* If the input is already a string, it is returned as-is.
|
|
284
|
+
*
|
|
285
|
+
* Use {@link toISO8601DayStringForSystem} when the system-local date is needed instead.
|
|
286
|
+
*
|
|
287
|
+
* @param dateOrString - date or existing day string
|
|
129
288
|
*
|
|
130
|
-
* @
|
|
131
|
-
*
|
|
289
|
+
* @example
|
|
290
|
+
* ```ts
|
|
291
|
+
* import { toISO8601DayStringForUTC } from '@dereekb/date';
|
|
292
|
+
*
|
|
293
|
+
* // When system is UTC-5, this date is Jan 16 in UTC
|
|
294
|
+
* toISO8601DayStringForUTC(new Date('2024-01-16T02:00:00Z'));
|
|
295
|
+
* // "2024-01-16"
|
|
296
|
+
*
|
|
297
|
+
* toISO8601DayStringForUTC('2024-01-15');
|
|
298
|
+
* // "2024-01-15" (pass-through)
|
|
299
|
+
* ```
|
|
132
300
|
*/
|
|
133
301
|
export declare function toISO8601DayStringForUTC(dateOrString: DateOrDayString): ISO8601DayString;
|
|
302
|
+
/**
|
|
303
|
+
* Formats a Date to an ISO 8601 day string (`yyyy-MM-dd`) using the **UTC date** components.
|
|
304
|
+
* Defaults to the current date.
|
|
305
|
+
*
|
|
306
|
+
* @param date - date to format; defaults to `new Date()`
|
|
307
|
+
*
|
|
308
|
+
* @example
|
|
309
|
+
* ```ts
|
|
310
|
+
* import { formatToISO8601DayStringForUTC } from '@dereekb/date';
|
|
311
|
+
*
|
|
312
|
+
* formatToISO8601DayStringForUTC(new Date('2024-01-15T12:00:00Z'));
|
|
313
|
+
* // "2024-01-15"
|
|
314
|
+
* ```
|
|
315
|
+
*/
|
|
134
316
|
export declare function formatToISO8601DayStringForUTC(date?: Date): ISO8601DayString;
|
|
317
|
+
/** date-fns format string for `MM/dd/yyyy` (e.g., `"01/15/2024"`). */
|
|
135
318
|
export declare const monthDaySlashDateStringFormat = "MM/dd/yyyy";
|
|
319
|
+
/**
|
|
320
|
+
* Formats a Date as a month/day/year slash-separated string (`MM/dd/yyyy`). Defaults to the current date.
|
|
321
|
+
*
|
|
322
|
+
* @param date - date to format; defaults to `new Date()`
|
|
323
|
+
*
|
|
324
|
+
* @example
|
|
325
|
+
* ```ts
|
|
326
|
+
* import { formatToMonthDaySlashDate } from '@dereekb/date';
|
|
327
|
+
*
|
|
328
|
+
* formatToMonthDaySlashDate(new Date('2024-01-15'));
|
|
329
|
+
* // "01/15/2024"
|
|
330
|
+
* ```
|
|
331
|
+
*/
|
|
136
332
|
export declare function formatToMonthDaySlashDate(date?: Date): MonthDaySlashDate;
|
|
137
333
|
/**
|
|
138
334
|
* @deprecated use formatToMonthDaySlashDate instead.
|
|
139
335
|
*/
|
|
140
336
|
export declare const formatToShortDateString: typeof formatToMonthDaySlashDate;
|
|
337
|
+
/** date-fns format string for `MM/dd` (e.g., `"01/15"`). */
|
|
141
338
|
export declare const dateMonthDayStringFormat = "MM/dd";
|
|
339
|
+
/**
|
|
340
|
+
* Formats a Date as a month/day string (`MM/dd`) without the year. Defaults to the current date.
|
|
341
|
+
*
|
|
342
|
+
* @param date - date to format; defaults to `new Date()`
|
|
343
|
+
*
|
|
344
|
+
* @example
|
|
345
|
+
* ```ts
|
|
346
|
+
* import { formatToMonthDayString } from '@dereekb/date';
|
|
347
|
+
*
|
|
348
|
+
* formatToMonthDayString(new Date('2024-01-15'));
|
|
349
|
+
* // "01/15"
|
|
350
|
+
* ```
|
|
351
|
+
*/
|
|
142
352
|
export declare function formatToMonthDayString(date?: Date): ISO8601DayString;
|
|
353
|
+
/**
|
|
354
|
+
* Formats a Date as a human-friendly weekday and month/day string (e.g., `"Mon, Jan 15th"`).
|
|
355
|
+
*
|
|
356
|
+
* @param date - date to format
|
|
357
|
+
*
|
|
358
|
+
* @example
|
|
359
|
+
* ```ts
|
|
360
|
+
* import { formatToDateString } from '@dereekb/date';
|
|
361
|
+
*
|
|
362
|
+
* formatToDateString(new Date('2024-01-15'));
|
|
363
|
+
* // "Mon, Jan 15th"
|
|
364
|
+
* ```
|
|
365
|
+
*/
|
|
143
366
|
export declare function formatToDateString(date: Date): string;
|
|
367
|
+
/** date-fns format string for 12-hour time with AM/PM (e.g., `"9:00 AM"`). */
|
|
144
368
|
export declare const dateTimeStringFormat = "h:mm a";
|
|
369
|
+
/**
|
|
370
|
+
* Formats a Date as a 12-hour time string with AM/PM (e.g., `"9:00 AM"`).
|
|
371
|
+
*
|
|
372
|
+
* @param date - date to format
|
|
373
|
+
*
|
|
374
|
+
* @example
|
|
375
|
+
* ```ts
|
|
376
|
+
* import { formatToTimeString } from '@dereekb/date';
|
|
377
|
+
*
|
|
378
|
+
* formatToTimeString(new Date('2024-01-15T14:30:00'));
|
|
379
|
+
* // "2:30 PM"
|
|
380
|
+
* ```
|
|
381
|
+
*/
|
|
145
382
|
export declare function formatToTimeString(date: Date): string;
|
|
383
|
+
/** Combined date-fns format string for `MM/dd/yyyy h:mm a` (e.g., `"01/15/2024 9:00 AM"`). */
|
|
146
384
|
export declare const dateShortDateAndTimeStringFormat = "MM/dd/yyyy h:mm a";
|
|
385
|
+
/**
|
|
386
|
+
* Formats a Date as a short date and time string (e.g., `"01/15/2024 9:00 AM"`).
|
|
387
|
+
*
|
|
388
|
+
* @param date - date to format
|
|
389
|
+
*
|
|
390
|
+
* @example
|
|
391
|
+
* ```ts
|
|
392
|
+
* import { formatToShortDateAndTimeString } from '@dereekb/date';
|
|
393
|
+
*
|
|
394
|
+
* formatToShortDateAndTimeString(new Date('2024-01-15T14:30:00'));
|
|
395
|
+
* // "01/15/2024 2:30 PM"
|
|
396
|
+
* ```
|
|
397
|
+
*/
|
|
147
398
|
export declare function formatToShortDateAndTimeString(date: Date): string;
|
|
399
|
+
/**
|
|
400
|
+
* Formats a start time with an appended duration indicator. For durations over 2 hours, uses
|
|
401
|
+
* a human-readable distance (e.g., `"about 3 hours"`); otherwise shows exact minutes.
|
|
402
|
+
*
|
|
403
|
+
* @param start - start date/time
|
|
404
|
+
* @param end - end date/time
|
|
405
|
+
*
|
|
406
|
+
* @example
|
|
407
|
+
* ```ts
|
|
408
|
+
* import { formatToTimeAndDurationString } from '@dereekb/date';
|
|
409
|
+
*
|
|
410
|
+
* // Short duration
|
|
411
|
+
* formatToTimeAndDurationString(new Date('2024-01-15T09:00:00'), new Date('2024-01-15T10:30:00'));
|
|
412
|
+
* // "9:00 AM (90 Minutes)"
|
|
413
|
+
*
|
|
414
|
+
* // Long duration
|
|
415
|
+
* formatToTimeAndDurationString(new Date('2024-01-15T09:00:00'), new Date('2024-01-15T14:00:00'));
|
|
416
|
+
* // "9:00 AM (about 5 hours)"
|
|
417
|
+
* ```
|
|
418
|
+
*/
|
|
148
419
|
export declare function formatToTimeAndDurationString(start: Date, end: Date): string;
|
|
420
|
+
/**
|
|
421
|
+
* Produces a relative-time label describing a date range's state relative to now:
|
|
422
|
+
* past (`"ended 2 hours ago"`), present (`"started 30 minutes ago"`), or future (`"starting in 3 days"`).
|
|
423
|
+
*
|
|
424
|
+
* @example
|
|
425
|
+
* ```ts
|
|
426
|
+
* import { formatStartedEndedDistanceString } from '@dereekb/date';
|
|
427
|
+
*
|
|
428
|
+
* // Range in the past
|
|
429
|
+
* const pastRange = { start: new Date('2024-01-10'), end: new Date('2024-01-12') };
|
|
430
|
+
* formatStartedEndedDistanceString(pastRange);
|
|
431
|
+
* // "ended about 1 year ago"
|
|
432
|
+
*
|
|
433
|
+
* // Range currently active
|
|
434
|
+
* const now = new Date();
|
|
435
|
+
* const activeRange = { start: new Date(now.getTime() - 3600000), end: new Date(now.getTime() + 3600000) };
|
|
436
|
+
* formatStartedEndedDistanceString(activeRange);
|
|
437
|
+
* // "started about 1 hour ago"
|
|
438
|
+
* ```
|
|
439
|
+
*/
|
|
149
440
|
export declare function formatStartedEndedDistanceString({ start, end }: DateRange): string;
|
|
150
441
|
/**
|
|
151
|
-
*
|
|
442
|
+
* Normalizes a Date or ISO 8601 day string to the start of the day in the system timezone.
|
|
443
|
+
* Useful for converting heterogeneous date inputs into a consistent midnight-aligned Date.
|
|
444
|
+
*
|
|
445
|
+
* @param input - date or day string to normalize
|
|
446
|
+
*
|
|
447
|
+
* @example
|
|
448
|
+
* ```ts
|
|
449
|
+
* import { toJsDayDate } from '@dereekb/date';
|
|
450
|
+
*
|
|
451
|
+
* toJsDayDate(new Date('2024-01-15T14:30:00'));
|
|
452
|
+
* // Date representing 2024-01-15T00:00:00 (system timezone)
|
|
152
453
|
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
454
|
+
* toJsDayDate('2024-01-15');
|
|
455
|
+
* // Date representing 2024-01-15T00:00:00 (system timezone)
|
|
456
|
+
* ```
|
|
155
457
|
*/
|
|
156
458
|
export declare function toJsDayDate(input: DateOrDayString): Date;
|
|
459
|
+
/**
|
|
460
|
+
* Parses an ISO 8601 day string (e.g., `"2024-01-15"`) or full date string into a Date at the start of the day
|
|
461
|
+
* in the system timezone. Supports year formats with more than 4 digits but does not support negative years.
|
|
462
|
+
*
|
|
463
|
+
* @param dayString - ISO 8601 day or date string to parse
|
|
464
|
+
*
|
|
465
|
+
* @example
|
|
466
|
+
* ```ts
|
|
467
|
+
* import { parseISO8601DayStringToDate } from '@dereekb/date';
|
|
468
|
+
*
|
|
469
|
+
* parseISO8601DayStringToDate('2024-01-15');
|
|
470
|
+
* // Date representing 2024-01-15T00:00:00 (system timezone)
|
|
471
|
+
*
|
|
472
|
+
* parseISO8601DayStringToDate('2024-01-15T14:30:00Z');
|
|
473
|
+
* // Date representing 2024-01-15T00:00:00 (system timezone, time portion stripped)
|
|
474
|
+
* ```
|
|
475
|
+
*/
|
|
157
476
|
export declare function parseISO8601DayStringToDate(dayString: ISO8601DayString | ISO8601DateString): Date;
|
|
@@ -1,4 +1,15 @@
|
|
|
1
1
|
import { HashSet } from '@dereekb/util';
|
|
2
|
+
/**
|
|
3
|
+
* A {@link HashSet} specialized for Date values, using the millisecond timestamp as the hash key.
|
|
4
|
+
*
|
|
5
|
+
* Provides O(1) lookup, add, and delete for Date values based on exact time equality.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const set = new DateSet([new Date('2024-01-01'), new Date('2024-01-02')]);
|
|
10
|
+
* set.has(new Date('2024-01-01')); // true (same timestamp)
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
2
13
|
export declare class DateSet extends HashSet<number, Date> {
|
|
3
14
|
constructor(values?: Date[]);
|
|
4
15
|
}
|
|
@@ -1,23 +1,81 @@
|
|
|
1
1
|
import { type DateNow, type Maybe, type FactoryWithInput } from '@dereekb/util';
|
|
2
|
+
/**
|
|
3
|
+
* String code for the start of the current day.
|
|
4
|
+
*/
|
|
2
5
|
export declare const DATE_TODAY_START_VALUE = "today_start";
|
|
3
6
|
export type DateTodayStart = typeof DATE_TODAY_START_VALUE;
|
|
7
|
+
/**
|
|
8
|
+
* String code for the end of the current day.
|
|
9
|
+
*/
|
|
4
10
|
export declare const DATE_TODAY_END_VALUE = "today_end";
|
|
5
11
|
export type DateTodayEnd = typeof DATE_TODAY_END_VALUE;
|
|
12
|
+
/**
|
|
13
|
+
* String code for the start of the current week.
|
|
14
|
+
*/
|
|
6
15
|
export declare const DATE_WEEK_START_VALUE = "this_week_start";
|
|
7
16
|
export type DateWeekStart = typeof DATE_WEEK_START_VALUE;
|
|
17
|
+
/**
|
|
18
|
+
* String code for the end of the current week.
|
|
19
|
+
*/
|
|
8
20
|
export declare const DATE_WEEK_END_VALUE = "this_week_end";
|
|
9
21
|
export type DateWeekEnd = typeof DATE_WEEK_END_VALUE;
|
|
22
|
+
/**
|
|
23
|
+
* Union of all recognized logical date string codes.
|
|
24
|
+
*/
|
|
10
25
|
export type LogicalDateStringCode = DateNow | DateTodayStart | DateTodayEnd | DateWeekStart | DateWeekEnd;
|
|
11
26
|
/**
|
|
12
|
-
* A date that is characterized by either a known string
|
|
27
|
+
* A date that is characterized by either a known string code or a concrete Date value.
|
|
28
|
+
*
|
|
29
|
+
* Used to express relative date references (e.g. "today_start") that resolve at evaluation time.
|
|
13
30
|
*/
|
|
14
31
|
export type LogicalDate = Date | LogicalDateStringCode;
|
|
32
|
+
/**
|
|
33
|
+
* Creates a factory function that resolves a {@link LogicalDateStringCode} to a concrete Date relative to an input reference date.
|
|
34
|
+
*
|
|
35
|
+
* @param logicalDateStringCode - the logical date code to resolve
|
|
36
|
+
* @returns a factory that accepts an optional reference date and returns the resolved Date
|
|
37
|
+
* @throws {Error} when the input code is not a recognized {@link LogicalDateStringCode}
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* const factory = logicalDateStringCodeDateFactory('today_start');
|
|
42
|
+
* const startOfToday = factory(new Date('2024-06-15T14:30:00Z'));
|
|
43
|
+
* // startOfToday is 2024-06-15T00:00:00 in the system timezone
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
15
46
|
export declare function logicalDateStringCodeDateFactory(logicalDateStringCode: LogicalDateStringCode): FactoryWithInput<Date, Date>;
|
|
16
47
|
/**
|
|
17
|
-
*
|
|
48
|
+
* Resolves a {@link LogicalDate} to a concrete Date value.
|
|
49
|
+
*
|
|
50
|
+
* If the input is a string code, it is resolved relative to the given reference date (or now).
|
|
51
|
+
* If the input is already a Date, it is returned as-is.
|
|
52
|
+
*
|
|
53
|
+
* @param logicalDate - the logical date to resolve (string code or Date)
|
|
54
|
+
* @param now - optional reference date (defaults to current time)
|
|
55
|
+
* @returns the resolved Date, or undefined/null if input is nullish
|
|
18
56
|
*
|
|
19
|
-
* @
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* const date = dateFromLogicalDate('today_end', new Date('2024-06-15T10:00:00Z'));
|
|
60
|
+
* // date is end of day 2024-06-15 in the system timezone
|
|
61
|
+
*
|
|
62
|
+
* const same = dateFromLogicalDate(new Date('2024-01-01'));
|
|
63
|
+
* // same is the exact Date passed in
|
|
64
|
+
* ```
|
|
20
65
|
*/
|
|
21
66
|
export declare function dateFromLogicalDate(logicalDate: LogicalDate, now?: Date): Date;
|
|
22
67
|
export declare function dateFromLogicalDate(logicalDate: Maybe<LogicalDate>, now?: Date): Maybe<Date>;
|
|
68
|
+
/**
|
|
69
|
+
* Type guard that checks whether the input is a recognized {@link LogicalDateStringCode}.
|
|
70
|
+
*
|
|
71
|
+
* @param logicalDate - value to check
|
|
72
|
+
* @returns true if the value is one of the known logical date string codes
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* isLogicalDateStringCode('today_start'); // true
|
|
77
|
+
* isLogicalDateStringCode('not_a_code'); // false
|
|
78
|
+
* isLogicalDateStringCode(new Date()); // false
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
23
81
|
export declare function isLogicalDateStringCode(logicalDate: Maybe<string | LogicalDate>): logicalDate is LogicalDateStringCode;
|