@trackunit/date-and-time-utils 0.0.25 → 0.0.27
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 +217 -47
- package/index.esm.d.ts +1 -0
- package/index.esm.js +211 -48
- package/package.json +3 -2
- package/src/DateAndTimeUtils.d.ts +94 -32
package/index.cjs.js
CHANGED
|
@@ -2,19 +2,43 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
+
require('@formatjs/intl-durationformat/polyfill');
|
|
6
|
+
var intlEnumerator = require('@formatjs/intl-enumerator');
|
|
5
7
|
var polyfill = require('@js-temporal/polyfill');
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
/**
|
|
10
|
+
* converts timZoneId to TimeZone object with default to current user timezone
|
|
11
|
+
*
|
|
12
|
+
* @param timeZoneId - The time zone to use for the conversion.
|
|
13
|
+
* returns A time zone object. { id: string, offset: string, name: string }
|
|
14
|
+
*/
|
|
15
|
+
const getTimeZone = (timeZoneId) => {
|
|
16
|
+
const tzId = timeZoneId !== null && timeZoneId !== void 0 ? timeZoneId : Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
17
|
+
const offset = getTimeZoneOffset(tzId);
|
|
18
|
+
return {
|
|
19
|
+
id: tzId,
|
|
20
|
+
offset: offset,
|
|
21
|
+
name: `${tzId} ${offset.length > 0 ? "(" + offset + ")" : ""}`,
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* get the offset count for a given timezone. Europe/Berlin would return +2:00
|
|
26
|
+
*
|
|
27
|
+
* @param timeZone - The time zone to use for the conversion.
|
|
28
|
+
*/
|
|
29
|
+
const getTimeZoneOffset = (timeZone) => {
|
|
30
|
+
var _a, _b;
|
|
31
|
+
const timezone = polyfill.Temporal.TimeZone.from(timeZone);
|
|
32
|
+
return (_b = (_a = timezone.getOffsetStringFor) === null || _a === void 0 ? void 0 : _a.call(timezone, polyfill.Temporal.Now.instant())) !== null && _b !== void 0 ? _b : "";
|
|
33
|
+
};
|
|
9
34
|
/**
|
|
10
35
|
* Returns a list of all supported time zones.
|
|
11
36
|
*
|
|
12
37
|
* @returns {string[]}
|
|
13
38
|
*/
|
|
14
|
-
|
|
15
|
-
const timeZonesAvailable = Intl.supportedValuesOf
|
|
39
|
+
const timeZonesAvailable = !intlEnumerator.shouldPolyfill()
|
|
16
40
|
? Intl.supportedValuesOf("timeZone")
|
|
17
|
-
:
|
|
41
|
+
: intlEnumerator.supportedValuesOf("timeZone");
|
|
18
42
|
/**
|
|
19
43
|
* Formats a temporal date according to the specified format, time zone, and locale.
|
|
20
44
|
*
|
|
@@ -56,43 +80,49 @@ const timeZonesAvailable = Intl.supportedValuesOf
|
|
|
56
80
|
* Output: "5/10/23, 12:00:00 AM EDT"
|
|
57
81
|
*/
|
|
58
82
|
const formatDateUtil = (date, format, timeZone, locale) => {
|
|
59
|
-
var _a, _b, _c, _d;
|
|
83
|
+
var _a, _b, _c, _d, _e, _f;
|
|
84
|
+
const dateObj = timeZone ? toZonedDateTimeUtil(date, timeZone) : toTemporalUtil(toDateUtil(date));
|
|
60
85
|
if ((format === null || format === void 0 ? void 0 : format.selectFormat) === "dateOnly") {
|
|
61
|
-
|
|
62
|
-
.toPlainDate()
|
|
63
|
-
|
|
64
|
-
|
|
86
|
+
if (dateObj instanceof polyfill.Temporal.ZonedDateTime) {
|
|
87
|
+
return dateObj.toPlainDate().toLocaleString(locale, {
|
|
88
|
+
dateStyle: (_a = format.dateFormat) !== null && _a !== void 0 ? _a : "medium",
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
return dateObj.toLocaleString(locale, {
|
|
92
|
+
dateStyle: (_b = format.dateFormat) !== null && _b !== void 0 ? _b : "medium",
|
|
65
93
|
});
|
|
66
94
|
}
|
|
67
95
|
if ((format === null || format === void 0 ? void 0 : format.selectFormat) === "timeOnly") {
|
|
68
|
-
|
|
69
|
-
.toPlainTime()
|
|
70
|
-
|
|
71
|
-
|
|
96
|
+
if (dateObj instanceof polyfill.Temporal.ZonedDateTime) {
|
|
97
|
+
return dateObj.toPlainTime().toLocaleString(locale, {
|
|
98
|
+
timeStyle: (_c = format.timeFormat) !== null && _c !== void 0 ? _c : "short",
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
return dateObj.toLocaleString(locale, {
|
|
102
|
+
timeStyle: (_d = format.timeFormat) !== null && _d !== void 0 ? _d : "short",
|
|
72
103
|
});
|
|
73
104
|
}
|
|
74
|
-
return
|
|
75
|
-
dateStyle: (
|
|
76
|
-
timeStyle: (
|
|
105
|
+
return dateObj.toLocaleString(locale, {
|
|
106
|
+
dateStyle: (_e = format === null || format === void 0 ? void 0 : format.dateFormat) !== null && _e !== void 0 ? _e : "medium",
|
|
107
|
+
timeStyle: (_f = format === null || format === void 0 ? void 0 : format.timeFormat) !== null && _f !== void 0 ? _f : "short",
|
|
77
108
|
});
|
|
78
109
|
};
|
|
79
110
|
/**
|
|
80
111
|
* Formats a date range.
|
|
81
112
|
*
|
|
82
|
-
* @param {
|
|
83
|
-
* @param {Date} end - The end date of the range.
|
|
113
|
+
* @param {DateRange} range - The start date of the range.
|
|
84
114
|
* @param {string} locale - The locale identifier (e.g., 'en-US', 'fr-FR') used for formatting.
|
|
85
115
|
* @param {Intl.DateTimeFormatOptions} [format] - Optional formatting options for the date and time.
|
|
86
116
|
* @returns {string} A formatted string representing the date range.
|
|
87
117
|
*/
|
|
88
|
-
const formatRangeUtil = (
|
|
118
|
+
const formatRangeUtil = (range, locale, format) => Intl.DateTimeFormat(locale, format).formatRange(range.start, range.end);
|
|
89
119
|
/**
|
|
90
120
|
* Subtracts a specified number of minutes from a date.
|
|
91
121
|
*
|
|
92
122
|
* @param {TemporalDate} date - The temporal date from which to subtract minutes.
|
|
93
123
|
* @param {number} amount - The number of minutes to subtract.
|
|
94
124
|
* @param {string} [timeZone] - The time zone to use for the subtraction. If not provided, the system's default time zone will be used.
|
|
95
|
-
* @returns {
|
|
125
|
+
* @returns {Temporal.ZonedDateTime} The resulting temporal date after subtracting the specified number of minutes.
|
|
96
126
|
* @example
|
|
97
127
|
* const dateString = "2023-05-10T10:30:00.000Z"
|
|
98
128
|
* const date = new Date(dateString);
|
|
@@ -109,7 +139,7 @@ const subtractMinutesUtil = (date, amount, timeZone) => {
|
|
|
109
139
|
* @param {TemporalDate} date - The temporal date from which to subtract hours.
|
|
110
140
|
* @param {number} amount - The number of hours to subtract.
|
|
111
141
|
* @param {string} [timeZone] - The time zone to use for the subtraction. If not provided, the system's default time zone will be used.
|
|
112
|
-
* @returns {
|
|
142
|
+
* @returns {Temporal.ZonedDateTime} The resulting temporal date after subtracting the specified number of hours.
|
|
113
143
|
* @example
|
|
114
144
|
* const dateString = "2023-05-10T10:30:00.000Z"
|
|
115
145
|
* const date = new Date(dateString);
|
|
@@ -126,7 +156,7 @@ const subtractHoursUtil = (date, amount, timeZone) => {
|
|
|
126
156
|
* @param {TemporalDate} date - The temporal date from which to subtract days.
|
|
127
157
|
* @param {number} amount - The number of days to subtract.
|
|
128
158
|
* @param {string} [timeZone] - The time zone to use for the subtraction. If not provided, the system's default time zone will be used.
|
|
129
|
-
* @returns {
|
|
159
|
+
* @returns {Temporal.ZonedDateTime} The resulting temporal date after subtracting the specified number of days.
|
|
130
160
|
* @example
|
|
131
161
|
* const dateString = "2023-05-10"
|
|
132
162
|
* const date = new Date(dateString);
|
|
@@ -143,7 +173,7 @@ const subtractDaysUtil = (date, amount, timeZone) => {
|
|
|
143
173
|
* @param {TemporalDate} date - The temporal date from which to subtract weeks.
|
|
144
174
|
* @param {number} amount - The number of weeks to subtract.
|
|
145
175
|
* @param {string} [timeZone] - The time zone to use for the subtraction. If not provided, the system's default time zone will be used.
|
|
146
|
-
* @returns {
|
|
176
|
+
* @returns {Temporal.ZonedDateTime} The resulting temporal date after subtracting the specified number of weeks.
|
|
147
177
|
* @example
|
|
148
178
|
* const dateString = "2023-05-10"
|
|
149
179
|
* const date = new Date(dateString);
|
|
@@ -160,7 +190,7 @@ const subtractWeeksUtil = (date, amount, timeZone) => {
|
|
|
160
190
|
* @param {TemporalDate} date - The temporal date from which to subtract months.
|
|
161
191
|
* @param {number} amount - The number of months to subtract.
|
|
162
192
|
* @param {string} [timeZone] - The time zone to use for the subtraction. If not provided, the system's default time zone will be used.
|
|
163
|
-
* @returns {
|
|
193
|
+
* @returns {Temporal.ZonedDateTime} The resulting temporal date after subtracting the specified number of months.
|
|
164
194
|
* @example
|
|
165
195
|
* const dateString = "2023-05-10"
|
|
166
196
|
* const date = new Date(dateString);
|
|
@@ -177,7 +207,7 @@ const subtractMonthsUtil = (date, amount, timeZone) => {
|
|
|
177
207
|
* @param {TemporalDate} date - The temporal date from which to subtract years.
|
|
178
208
|
* @param {number} amount - The number of years to subtract.
|
|
179
209
|
* @param {string} [timeZone] - The time zone to use for the subtraction. If not provided, the system's default time zone will be used.
|
|
180
|
-
* @returns {
|
|
210
|
+
* @returns {Temporal.ZonedDateTime} The resulting temporal date after subtracting the specified number of years.
|
|
181
211
|
* @example
|
|
182
212
|
* const dateString = "2023-05-10"
|
|
183
213
|
* const date = new Date(dateString);
|
|
@@ -194,7 +224,7 @@ const subtractYearsUtil = (date, amount, timeZone) => {
|
|
|
194
224
|
* @param {TemporalDate} date - The temporal date to which minutes will be added.
|
|
195
225
|
* @param {number} amount - The number of minutes to add.
|
|
196
226
|
* @param {string} [timeZone] - The time zone to use for the addition. If not provided, the system's default time zone will be used.
|
|
197
|
-
* @returns {
|
|
227
|
+
* @returns {Temporal.ZonedDateTime} The resulting temporal date after adding the specified number of minutes.
|
|
198
228
|
* @example
|
|
199
229
|
* const dateString = "2023-05-10"
|
|
200
230
|
* const date = new Date(dateString);
|
|
@@ -211,7 +241,7 @@ const addMinutesUtil = (date, amount, timeZone) => {
|
|
|
211
241
|
* @param {TemporalDate} date - The temporal date to which hours will be added.
|
|
212
242
|
* @param {number} amount - The number of hours to add.
|
|
213
243
|
* @param {string} [timeZone] - The time zone to use for the addition. If not provided, the system's default time zone will be used.
|
|
214
|
-
* @returns {
|
|
244
|
+
* @returns {Temporal.ZonedDateTime} The resulting temporal date after adding the specified number of hours.
|
|
215
245
|
* @example
|
|
216
246
|
* const dateString = "2023-05-10"
|
|
217
247
|
* const date = new Date(dateString);
|
|
@@ -228,7 +258,7 @@ const addHoursUtil = (date, amount, timeZone) => {
|
|
|
228
258
|
* @param {TemporalDate} date - The temporal date to which days will be added.
|
|
229
259
|
* @param {number} amount - The number of days to add.
|
|
230
260
|
* @param {string} [timeZone] - The time zone to use for the addition. If not provided, the system's default time zone will be used.
|
|
231
|
-
* @returns {
|
|
261
|
+
* @returns {Temporal.ZonedDateTime} The resulting temporal date after adding the specified number of days.
|
|
232
262
|
* @example
|
|
233
263
|
* const dateString = "2023-05-10"
|
|
234
264
|
* const date = new Date(dateString);
|
|
@@ -245,7 +275,7 @@ const addDaysUtil = (date, amount, timeZone) => {
|
|
|
245
275
|
* @param {TemporalDate} date - The temporal date to which weeks will be added.
|
|
246
276
|
* @param {number} amount - The number of weeks to add.
|
|
247
277
|
* @param {string} [timeZone] - The time zone to use for the addition. If not provided, the system's default time zone will be used.
|
|
248
|
-
* @returns {
|
|
278
|
+
* @returns {Temporal.ZonedDateTime} The resulting temporal date after adding the specified number of weeks.
|
|
249
279
|
* @example
|
|
250
280
|
* const dateString = "2023-05-10"
|
|
251
281
|
* const date = new Date(dateString);
|
|
@@ -262,7 +292,7 @@ const addWeeksUtil = (date, amount, timeZone) => {
|
|
|
262
292
|
* @param {TemporalDate} date - The temporal date to which months will be added.
|
|
263
293
|
* @param {number} amount - The number of months to add.
|
|
264
294
|
* @param {string} [timeZone] - The time zone to use for the addition. If not provided, the system's default time zone will be used.
|
|
265
|
-
* @returns {
|
|
295
|
+
* @returns {Temporal.ZonedDateTime} The resulting temporal date after adding the specified number of months.
|
|
266
296
|
* @example
|
|
267
297
|
* const dateString = "2023-05-10"
|
|
268
298
|
* const date = new Date(dateString);
|
|
@@ -279,7 +309,7 @@ const addMonthsUtil = (date, amount, timeZone) => {
|
|
|
279
309
|
* @param {TemporalDate} date - The temporal date to which years will be added.
|
|
280
310
|
* @param {number} amount - The number of years to add.
|
|
281
311
|
* @param {string} [timeZone] - The time zone to use for the addition. If not provided, the system's default time zone will be used.
|
|
282
|
-
* @returns {
|
|
312
|
+
* @returns {Temporal.ZonedDateTime} The resulting temporal date after adding the specified number of years.
|
|
283
313
|
* @example
|
|
284
314
|
* const dateString = "2023-05-10"
|
|
285
315
|
* const date = new Date(dateString);
|
|
@@ -295,7 +325,7 @@ const addYearsUtil = (date, amount, timeZone) => {
|
|
|
295
325
|
*
|
|
296
326
|
* @param {TemporalDate} date - The temporal date for which to determine the start of the minute.
|
|
297
327
|
* @param {string} [timeZone] - The time zone to use for determining the start of the minute. If not provided, the system's default time zone will be used.
|
|
298
|
-
* @returns {
|
|
328
|
+
* @returns {Temporal.ZonedDateTime} The temporal date representing the start of the minute.
|
|
299
329
|
* @example
|
|
300
330
|
* const dateString = "2023-05-10T10:30:10.000Z"
|
|
301
331
|
* const date = new Date(dateString);
|
|
@@ -310,7 +340,7 @@ const startOfMinuteUtil = (date, timeZone) => {
|
|
|
310
340
|
*
|
|
311
341
|
* @param {TemporalDate} date - The temporal date for which to determine the end of the minute.
|
|
312
342
|
* @param {string} [timeZone] - The time zone to use for determining the end of the minute. If not provided, the system's default time zone will be used.
|
|
313
|
-
* @returns {
|
|
343
|
+
* @returns {Temporal.ZonedDateTime} The temporal date representing the end of the minute.
|
|
314
344
|
* @example
|
|
315
345
|
* const dateString = "2023-05-10T10:30:10.000Z"
|
|
316
346
|
* const date = new Date(dateString);
|
|
@@ -328,7 +358,7 @@ const endOfMinuteUtil = (date, timeZone) => {
|
|
|
328
358
|
*
|
|
329
359
|
* @param {TemporalDate} date - The temporal date for which to determine the start of the hour.
|
|
330
360
|
* @param {string} [timeZone] - The time zone to use for determining the start of the hour. If not provided, the system's default time zone will be used.
|
|
331
|
-
* @returns {
|
|
361
|
+
* @returns {Temporal.ZonedDateTime} The temporal date representing the start of the hour.
|
|
332
362
|
* @example
|
|
333
363
|
* const dateString = "2023-05-10T10:30:30.000Z"
|
|
334
364
|
* const date = new Date(dateString);
|
|
@@ -343,7 +373,7 @@ const startOfHourUtil = (date, timeZone) => {
|
|
|
343
373
|
*
|
|
344
374
|
* @param {TemporalDate} date - The temporal date for which to determine the end of the hour.
|
|
345
375
|
* @param {string} [timeZone] - The time zone to use for determining the end of the hour. If not provided, the system's default time zone will be used.
|
|
346
|
-
* @returns {
|
|
376
|
+
* @returns {Temporal.ZonedDateTime} The temporal date representing the end of the hour.
|
|
347
377
|
* @example
|
|
348
378
|
* const dateString = "2023-05-10T10:30:00.000Z"
|
|
349
379
|
* const date = new Date(dateString);
|
|
@@ -361,7 +391,7 @@ const endOfHourUtil = (date, timeZone) => {
|
|
|
361
391
|
*
|
|
362
392
|
* @param {TemporalDate} date - The temporal date for which to determine the start of the day.
|
|
363
393
|
* @param {string} [timeZone] - The time zone to use for determining the start of the day. If not provided, the system's default time zone will be used.
|
|
364
|
-
* @returns {
|
|
394
|
+
* @returns {Temporal.ZonedDateTime} The temporal date representing the start of the day.
|
|
365
395
|
* @example
|
|
366
396
|
* const dateString = "2023-05-10T10:30:00.000Z"
|
|
367
397
|
* const date = new Date(dateString);
|
|
@@ -376,7 +406,7 @@ const startOfDayUtil = (date, timeZone) => {
|
|
|
376
406
|
*
|
|
377
407
|
* @param {TemporalDate} date - The temporal date for which to determine the end of the day.
|
|
378
408
|
* @param {string} [timeZone] - The time zone to use for determining the end of the day. If not provided, the system's default time zone will be used.
|
|
379
|
-
* @returns {
|
|
409
|
+
* @returns {Temporal.ZonedDateTime} The temporal date representing the end of the day.
|
|
380
410
|
* @example
|
|
381
411
|
* const dateString = "2023-05-10T10:30:00.000Z"
|
|
382
412
|
* const date = new Date(dateString);
|
|
@@ -384,9 +414,7 @@ const startOfDayUtil = (date, timeZone) => {
|
|
|
384
414
|
* Output: Temporal.ZonedDateTime { year: 2023, month: 5, day: 10, hour: 23, minute: 59, second: 59, millisecond: 999, microsecond: 0, nanosecond: 0, timeZone: Temporal.TimeZone { id: "America/New_York" }, calendar: Temporal.Calendar { id: "iso8601" } }
|
|
385
415
|
*/
|
|
386
416
|
const endOfDayUtil = (date, timeZone) => {
|
|
387
|
-
const day = toZonedDateTimeUtil(date, timeZone)
|
|
388
|
-
.startOfDay()
|
|
389
|
-
.subtract(polyfill.Temporal.Duration.from({ milliseconds: 1 }));
|
|
417
|
+
const day = toZonedDateTimeUtil(date, timeZone).startOfDay().subtract({ seconds: 1 });
|
|
390
418
|
return addDaysUtil(day, 1);
|
|
391
419
|
};
|
|
392
420
|
/**
|
|
@@ -394,7 +422,7 @@ const endOfDayUtil = (date, timeZone) => {
|
|
|
394
422
|
*
|
|
395
423
|
* @param {TemporalDate} date - The temporal date for which to determine the start of the week.
|
|
396
424
|
* @param {string} [timeZone] - The time zone to use for determining the start of the week. If not provided, the system's default time zone will be used.
|
|
397
|
-
* @returns {
|
|
425
|
+
* @returns {Temporal.ZonedDateTime} The temporal date representing the start of the week.
|
|
398
426
|
* @example
|
|
399
427
|
* const dateString = "2023-05-10T10:30:00.000Z"
|
|
400
428
|
* const date = new Date(dateString);
|
|
@@ -411,7 +439,7 @@ const startOfWeekUtil = (date, timeZone) => {
|
|
|
411
439
|
*
|
|
412
440
|
* @param {TemporalDate} date - The temporal date for which to determine the end of the week.
|
|
413
441
|
* @param {string} [timeZone] - The time zone to use for determining the end of the week. If not provided, the system's default time zone will be used.
|
|
414
|
-
* @returns {
|
|
442
|
+
* @returns {Temporal.ZonedDateTime} The temporal date representing the end of the week.
|
|
415
443
|
* @example
|
|
416
444
|
* const dateString = "2023-05-10T10:30:00.000Z"
|
|
417
445
|
* const date = new Date(dateString);
|
|
@@ -429,7 +457,7 @@ const endOfWeekUtil = (date, timeZone) => {
|
|
|
429
457
|
*
|
|
430
458
|
* @param {TemporalDate} date - The temporal date for which to determine the start of the month.
|
|
431
459
|
* @param {string} [timeZone] - The time zone to use for determining the start of the month. If not provided, the system's default time zone will be used.
|
|
432
|
-
* @returns {
|
|
460
|
+
* @returns {Temporal.ZonedDateTime} The temporal date representing the start of the month.
|
|
433
461
|
* @example
|
|
434
462
|
* const dateString = "2023-05-10T10:30:00.000Z"
|
|
435
463
|
* const date = new Date(dateString);
|
|
@@ -446,7 +474,7 @@ const startOfMonthUtil = (date, timeZone) => {
|
|
|
446
474
|
*
|
|
447
475
|
* @param {TemporalDate} date - The temporal date for which to determine the end of the month.
|
|
448
476
|
* @param {string} [timeZone] - The time zone to use for determining the end of the month. If not provided, the system's default time zone will be used.
|
|
449
|
-
* @returns {
|
|
477
|
+
* @returns {Temporal.ZonedDateTime} The temporal date representing the end of the month.
|
|
450
478
|
* @example
|
|
451
479
|
* const dateString = "2023-05-10"
|
|
452
480
|
* const date = new Date(dateString);
|
|
@@ -640,6 +668,22 @@ const isPastUtil = (date) => {
|
|
|
640
668
|
const isTodayUtil = (date) => {
|
|
641
669
|
return polyfill.Temporal.PlainDate.compare(toZonedDateTimeUtil(polyfill.Temporal.Now.instant()), toZonedDateTimeUtil(date)) === 0;
|
|
642
670
|
};
|
|
671
|
+
/**
|
|
672
|
+
*
|
|
673
|
+
*
|
|
674
|
+
* @param date - The date to check.
|
|
675
|
+
* @param start - The start date to compare against.
|
|
676
|
+
* @param end - The en date to compare against.
|
|
677
|
+
*/
|
|
678
|
+
const isBetweenUtil = (date, start, end) => {
|
|
679
|
+
if (!start || !end) {
|
|
680
|
+
throw new Error("Start and end dates must be provided");
|
|
681
|
+
}
|
|
682
|
+
const dateToCheck = new Date(date.toLocaleString());
|
|
683
|
+
const startDate = new Date(start.toLocaleString());
|
|
684
|
+
const endDate = new Date(end.toLocaleString());
|
|
685
|
+
return dateToCheck >= startDate && dateToCheck <= endDate;
|
|
686
|
+
};
|
|
643
687
|
/**
|
|
644
688
|
* Checks if a date is same day as current.
|
|
645
689
|
*
|
|
@@ -706,7 +750,9 @@ const isSameYearUtil = (date) => {
|
|
|
706
750
|
* Output: true
|
|
707
751
|
*/
|
|
708
752
|
const isEqualUtil = (from, to) => {
|
|
709
|
-
return toZonedDateTimeUtil(from)
|
|
753
|
+
return toZonedDateTimeUtil(from)
|
|
754
|
+
.round({ smallestUnit: "seconds" })
|
|
755
|
+
.equals(toZonedDateTimeUtil(to).round({ smallestUnit: "seconds" }));
|
|
710
756
|
};
|
|
711
757
|
/**
|
|
712
758
|
* Calculates the time duration between two TemporalDate objects and returns a human-readable string representation of the duration.
|
|
@@ -740,6 +786,9 @@ const timeSinceAuto = (from, to, timeZone, locale) => {
|
|
|
740
786
|
largestUnit: "years",
|
|
741
787
|
smallestUnit: "seconds",
|
|
742
788
|
}));
|
|
789
|
+
if (duration.blank) {
|
|
790
|
+
return getRelativeTimeFormat(locale).format(-1, "seconds");
|
|
791
|
+
}
|
|
743
792
|
if (duration.years !== 0) {
|
|
744
793
|
return timeSinceInYears(from, to, timeZone, locale);
|
|
745
794
|
}
|
|
@@ -866,6 +915,10 @@ const timeSinceInDays = (from, to, timeZone, locale) => {
|
|
|
866
915
|
* Output: "2 months ago"
|
|
867
916
|
*/
|
|
868
917
|
const timeSinceInMonths = (from, to, timeZone, locale) => {
|
|
918
|
+
const isLeapYear = toZonedDateTimeUtil(from, timeZone).inLeapYear && toZonedDateTimeUtil(to, timeZone).inLeapYear;
|
|
919
|
+
if (isLeapYear) {
|
|
920
|
+
return timeSinceInDays(from, to, timeZone, locale);
|
|
921
|
+
}
|
|
869
922
|
const duration = polyfill.Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
|
|
870
923
|
largestUnit: "months",
|
|
871
924
|
}));
|
|
@@ -887,12 +940,75 @@ const timeSinceInMonths = (from, to, timeZone, locale) => {
|
|
|
887
940
|
* Output: "1 year ago"
|
|
888
941
|
*/
|
|
889
942
|
const timeSinceInYears = (from, to, timeZone, locale) => {
|
|
943
|
+
const isLeapYear = toZonedDateTimeUtil(from, timeZone).inLeapYear && toZonedDateTimeUtil(to, timeZone).inLeapYear;
|
|
944
|
+
if (isLeapYear) {
|
|
945
|
+
return timeSinceInDays(from, to, timeZone, locale);
|
|
946
|
+
}
|
|
890
947
|
const duration = polyfill.Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
|
|
891
948
|
largestUnit: "years",
|
|
892
949
|
}));
|
|
893
950
|
return getRelativeTimeFormat(locale).format(duration.years, "years");
|
|
894
951
|
};
|
|
895
952
|
const getRelativeTimeFormat = (locale) => new Intl.RelativeTimeFormat(locale, { style: "long", numeric: "always" });
|
|
953
|
+
/**
|
|
954
|
+
* Get duration in human-readable form
|
|
955
|
+
*
|
|
956
|
+
* @param duration - years, months, days, minutes, seconds
|
|
957
|
+
* @param locale - selected locale
|
|
958
|
+
* @param format - long, short, narrow
|
|
959
|
+
* @returns {string} human-readable string
|
|
960
|
+
*/
|
|
961
|
+
const getDurationFormat = (duration, locale, format) => {
|
|
962
|
+
return new Intl.DurationFormat(locale, { style: format !== null && format !== void 0 ? format : "long" }).format(duration);
|
|
963
|
+
};
|
|
964
|
+
/**
|
|
965
|
+
* convert a number to a type
|
|
966
|
+
*
|
|
967
|
+
* @param {number} number - 230.45
|
|
968
|
+
* @param {TemporalTimeTypes} unit - "days | "hours" | minutes
|
|
969
|
+
* @param {Temporal.TimeUnit} roundAt - smallest unit to show
|
|
970
|
+
*/
|
|
971
|
+
const toDuration = (number, unit, roundAt) => {
|
|
972
|
+
switch (unit) {
|
|
973
|
+
case "days":
|
|
974
|
+
return polyfill.Temporal.Duration.from(durationBuilder(number, "days")).round({
|
|
975
|
+
smallestUnit: roundAt !== null && roundAt !== void 0 ? roundAt : "seconds",
|
|
976
|
+
});
|
|
977
|
+
case "hours":
|
|
978
|
+
return polyfill.Temporal.Duration.from(durationBuilder(number, "hours")).round({ smallestUnit: roundAt !== null && roundAt !== void 0 ? roundAt : "minutes" });
|
|
979
|
+
case "minutes":
|
|
980
|
+
return polyfill.Temporal.Duration.from(durationBuilder(number, "minutes")).round({
|
|
981
|
+
smallestUnit: roundAt !== null && roundAt !== void 0 ? roundAt : "seconds",
|
|
982
|
+
});
|
|
983
|
+
default:
|
|
984
|
+
throw new Error("Not a valid unit", { cause: unit });
|
|
985
|
+
}
|
|
986
|
+
};
|
|
987
|
+
const durationBuilder = (number, unit) => {
|
|
988
|
+
const days = Math.floor(number);
|
|
989
|
+
const hours = unit === "hours" ? Math.floor(number) : Math.floor((number * 24) % 24);
|
|
990
|
+
const minutes = unit === "minutes" ? Math.floor(number) : Math.floor((number * 24 * 60) % 60);
|
|
991
|
+
const seconds = Math.floor((number * 24 * 60 * 60) % 60);
|
|
992
|
+
switch (unit) {
|
|
993
|
+
case "days":
|
|
994
|
+
if (Number.isSafeInteger(number)) {
|
|
995
|
+
return `P${number}D`;
|
|
996
|
+
}
|
|
997
|
+
return `P${days}DT${hours}H${minutes}M${seconds}S`;
|
|
998
|
+
case "hours":
|
|
999
|
+
if (Number.isSafeInteger(number)) {
|
|
1000
|
+
return `PT${number}H`;
|
|
1001
|
+
}
|
|
1002
|
+
return `PT${hours}H${minutes}M${seconds}S`;
|
|
1003
|
+
case "minutes":
|
|
1004
|
+
if (Number.isSafeInteger(number)) {
|
|
1005
|
+
return `PT${number}M`;
|
|
1006
|
+
}
|
|
1007
|
+
return `PT${minutes}M${seconds}S`;
|
|
1008
|
+
default:
|
|
1009
|
+
return `PT0S`;
|
|
1010
|
+
}
|
|
1011
|
+
};
|
|
896
1012
|
/**
|
|
897
1013
|
* Converts a JavaScript Date object to a Temporal Instant.
|
|
898
1014
|
*
|
|
@@ -904,6 +1020,9 @@ const getRelativeTimeFormat = (locale) => new Intl.RelativeTimeFormat(locale, {
|
|
|
904
1020
|
* Output: Temporal.Instant { year: ..., month: ..., day: ..., hour: ..., minute: ..., second: ..., millisecond: ... }
|
|
905
1021
|
*/
|
|
906
1022
|
const toTemporalUtil = (date) => {
|
|
1023
|
+
if (typeof date === "string") {
|
|
1024
|
+
return polyfill.Temporal.Instant.from(date);
|
|
1025
|
+
}
|
|
907
1026
|
return polyfill.Temporal.Instant.from(date.toISOString());
|
|
908
1027
|
};
|
|
909
1028
|
/**
|
|
@@ -922,6 +1041,12 @@ const toTemporalUtil = (date) => {
|
|
|
922
1041
|
* Output: Date { ... } (equivalent JavaScript Date object)
|
|
923
1042
|
*/
|
|
924
1043
|
const toDateUtil = (date) => {
|
|
1044
|
+
if (typeof date === "string" || typeof date === "number") {
|
|
1045
|
+
return new Date(date);
|
|
1046
|
+
}
|
|
1047
|
+
if (date instanceof Date) {
|
|
1048
|
+
return date;
|
|
1049
|
+
}
|
|
925
1050
|
if (date instanceof polyfill.Temporal.ZonedDateTime) {
|
|
926
1051
|
date = toInstantUtil(date);
|
|
927
1052
|
}
|
|
@@ -938,6 +1063,12 @@ const toDateUtil = (date) => {
|
|
|
938
1063
|
* Output: Temporal.Instant { year: ..., month: ..., day: ..., hour: ..., minute: ..., second: ..., millisecond: ... }
|
|
939
1064
|
*/
|
|
940
1065
|
const toInstantUtil = (zonedDateTime) => {
|
|
1066
|
+
if (typeof zonedDateTime === "string") {
|
|
1067
|
+
return polyfill.Temporal.Instant.from(zonedDateTime);
|
|
1068
|
+
}
|
|
1069
|
+
if (zonedDateTime instanceof Date) {
|
|
1070
|
+
return polyfill.Temporal.Instant.from(zonedDateTime.toString());
|
|
1071
|
+
}
|
|
941
1072
|
return zonedDateTime.toInstant();
|
|
942
1073
|
};
|
|
943
1074
|
/**
|
|
@@ -958,7 +1089,7 @@ const toZonedDateTimeUtil = (date, timeZoneId, calendar) => {
|
|
|
958
1089
|
if (date instanceof polyfill.Temporal.ZonedDateTime) {
|
|
959
1090
|
return date;
|
|
960
1091
|
}
|
|
961
|
-
if (date instanceof Date) {
|
|
1092
|
+
if (date instanceof Date || typeof date === "string") {
|
|
962
1093
|
date = toTemporalUtil(date);
|
|
963
1094
|
}
|
|
964
1095
|
return date.toZonedDateTime({
|
|
@@ -994,6 +1125,22 @@ const monthNameUtil = (date, format, locale) => {
|
|
|
994
1125
|
month: format,
|
|
995
1126
|
});
|
|
996
1127
|
};
|
|
1128
|
+
/**
|
|
1129
|
+
* Returns a list of month names
|
|
1130
|
+
*
|
|
1131
|
+
* @param {TemporalFormatStyle} format - select the format of the month name
|
|
1132
|
+
* @param {string} locale - The locale to use for formatting. If not provided, the system's default locale will be used.
|
|
1133
|
+
*/
|
|
1134
|
+
const monthsUtil = (format, locale) => {
|
|
1135
|
+
const months = [];
|
|
1136
|
+
const d = new Date();
|
|
1137
|
+
const monthsInYear = polyfill.Temporal.PlainDate.from(d.toISOString()).monthsInYear;
|
|
1138
|
+
for (let month = 0; month < monthsInYear; ++month) {
|
|
1139
|
+
d.setMonth(month);
|
|
1140
|
+
months.push(d.toLocaleString(locale, { month: format !== null && format !== void 0 ? format : "long" }));
|
|
1141
|
+
}
|
|
1142
|
+
return months;
|
|
1143
|
+
};
|
|
997
1144
|
/**
|
|
998
1145
|
* Returns the day name for the given date.
|
|
999
1146
|
*
|
|
@@ -1022,6 +1169,22 @@ const dayNameUtil = (date, format, locale) => {
|
|
|
1022
1169
|
weekday: format,
|
|
1023
1170
|
});
|
|
1024
1171
|
};
|
|
1172
|
+
/**
|
|
1173
|
+
* Returns a list of day names
|
|
1174
|
+
*
|
|
1175
|
+
* @param {TemporalFormatStyle} format - select the format of the day name
|
|
1176
|
+
* @param {string} locale - The locale to use for formatting. If not provided, the system's default locale will be used.
|
|
1177
|
+
*/
|
|
1178
|
+
const daysUtil = (format, locale) => {
|
|
1179
|
+
const days = [];
|
|
1180
|
+
const d = new Date();
|
|
1181
|
+
const daysInWeek = polyfill.Temporal.PlainDate.from(d.toISOString()).daysInWeek;
|
|
1182
|
+
for (let day = 0; day < daysInWeek; ++day) {
|
|
1183
|
+
d.setDate(day);
|
|
1184
|
+
days.push(d.toLocaleString(locale, { weekday: format !== null && format !== void 0 ? format : "long" }));
|
|
1185
|
+
}
|
|
1186
|
+
return days;
|
|
1187
|
+
};
|
|
1025
1188
|
|
|
1026
1189
|
exports.addDaysUtil = addDaysUtil;
|
|
1027
1190
|
exports.addHoursUtil = addHoursUtil;
|
|
@@ -1030,6 +1193,7 @@ exports.addMonthsUtil = addMonthsUtil;
|
|
|
1030
1193
|
exports.addWeeksUtil = addWeeksUtil;
|
|
1031
1194
|
exports.addYearsUtil = addYearsUtil;
|
|
1032
1195
|
exports.dayNameUtil = dayNameUtil;
|
|
1196
|
+
exports.daysUtil = daysUtil;
|
|
1033
1197
|
exports.differenceInDaysUtil = differenceInDaysUtil;
|
|
1034
1198
|
exports.differenceInHoursUtil = differenceInHoursUtil;
|
|
1035
1199
|
exports.differenceInMinutesUtil = differenceInMinutesUtil;
|
|
@@ -1042,6 +1206,10 @@ exports.endOfMonthUtil = endOfMonthUtil;
|
|
|
1042
1206
|
exports.endOfWeekUtil = endOfWeekUtil;
|
|
1043
1207
|
exports.formatDateUtil = formatDateUtil;
|
|
1044
1208
|
exports.formatRangeUtil = formatRangeUtil;
|
|
1209
|
+
exports.getDurationFormat = getDurationFormat;
|
|
1210
|
+
exports.getTimeZone = getTimeZone;
|
|
1211
|
+
exports.getTimeZoneOffset = getTimeZoneOffset;
|
|
1212
|
+
exports.isBetweenUtil = isBetweenUtil;
|
|
1045
1213
|
exports.isEqualUtil = isEqualUtil;
|
|
1046
1214
|
exports.isFutureUtil = isFutureUtil;
|
|
1047
1215
|
exports.isPastUtil = isPastUtil;
|
|
@@ -1051,6 +1219,7 @@ exports.isSameWeekUtil = isSameWeekUtil;
|
|
|
1051
1219
|
exports.isSameYearUtil = isSameYearUtil;
|
|
1052
1220
|
exports.isTodayUtil = isTodayUtil;
|
|
1053
1221
|
exports.monthNameUtil = monthNameUtil;
|
|
1222
|
+
exports.monthsUtil = monthsUtil;
|
|
1054
1223
|
exports.startOfDayUtil = startOfDayUtil;
|
|
1055
1224
|
exports.startOfHourUtil = startOfHourUtil;
|
|
1056
1225
|
exports.startOfMinuteUtil = startOfMinuteUtil;
|
|
@@ -1071,6 +1240,7 @@ exports.timeSinceInSeconds = timeSinceInSeconds;
|
|
|
1071
1240
|
exports.timeSinceInYears = timeSinceInYears;
|
|
1072
1241
|
exports.timeZonesAvailable = timeZonesAvailable;
|
|
1073
1242
|
exports.toDateUtil = toDateUtil;
|
|
1243
|
+
exports.toDuration = toDuration;
|
|
1074
1244
|
exports.toInstantUtil = toInstantUtil;
|
|
1075
1245
|
exports.toTemporalUtil = toTemporalUtil;
|
|
1076
1246
|
exports.toZonedDateTimeUtil = toZonedDateTimeUtil;
|
package/index.esm.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/index";
|