@trackunit/date-and-time-utils 0.0.26 → 0.0.28-alpha-26d41cfe4c9.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 +217 -47
- package/index.esm.js +211 -48
- package/package.json +4 -3
- package/src/DateAndTimeUtils.d.ts +94 -32
package/index.esm.js
CHANGED
|
@@ -1,16 +1,40 @@
|
|
|
1
|
+
import '@formatjs/intl-durationformat/polyfill';
|
|
2
|
+
import { shouldPolyfill, supportedValuesOf } from '@formatjs/intl-enumerator';
|
|
1
3
|
import { Temporal } from '@js-temporal/polyfill';
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
+
/**
|
|
6
|
+
* converts timZoneId to TimeZone object with default to current user timezone
|
|
7
|
+
*
|
|
8
|
+
* @param timeZoneId - The time zone to use for the conversion.
|
|
9
|
+
* returns A time zone object. { id: string, offset: string, name: string }
|
|
10
|
+
*/
|
|
11
|
+
const getTimeZone = (timeZoneId) => {
|
|
12
|
+
const tzId = timeZoneId !== null && timeZoneId !== void 0 ? timeZoneId : Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
13
|
+
const offset = getTimeZoneOffset(tzId);
|
|
14
|
+
return {
|
|
15
|
+
id: tzId,
|
|
16
|
+
offset: offset,
|
|
17
|
+
name: `${tzId} ${offset.length > 0 ? "(" + offset + ")" : ""}`,
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* get the offset count for a given timezone. Europe/Berlin would return +2:00
|
|
22
|
+
*
|
|
23
|
+
* @param timeZone - The time zone to use for the conversion.
|
|
24
|
+
*/
|
|
25
|
+
const getTimeZoneOffset = (timeZone) => {
|
|
26
|
+
var _a, _b;
|
|
27
|
+
const timezone = Temporal.TimeZone.from(timeZone);
|
|
28
|
+
return (_b = (_a = timezone.getOffsetStringFor) === null || _a === void 0 ? void 0 : _a.call(timezone, Temporal.Now.instant())) !== null && _b !== void 0 ? _b : "";
|
|
29
|
+
};
|
|
5
30
|
/**
|
|
6
31
|
* Returns a list of all supported time zones.
|
|
7
32
|
*
|
|
8
33
|
* @returns {string[]}
|
|
9
34
|
*/
|
|
10
|
-
|
|
11
|
-
const timeZonesAvailable = Intl.supportedValuesOf
|
|
35
|
+
const timeZonesAvailable = !shouldPolyfill()
|
|
12
36
|
? Intl.supportedValuesOf("timeZone")
|
|
13
|
-
:
|
|
37
|
+
: supportedValuesOf("timeZone");
|
|
14
38
|
/**
|
|
15
39
|
* Formats a temporal date according to the specified format, time zone, and locale.
|
|
16
40
|
*
|
|
@@ -52,43 +76,49 @@ const timeZonesAvailable = Intl.supportedValuesOf
|
|
|
52
76
|
* Output: "5/10/23, 12:00:00 AM EDT"
|
|
53
77
|
*/
|
|
54
78
|
const formatDateUtil = (date, format, timeZone, locale) => {
|
|
55
|
-
var _a, _b, _c, _d;
|
|
79
|
+
var _a, _b, _c, _d, _e, _f;
|
|
80
|
+
const dateObj = timeZone ? toZonedDateTimeUtil(date, timeZone) : toTemporalUtil(toDateUtil(date));
|
|
56
81
|
if ((format === null || format === void 0 ? void 0 : format.selectFormat) === "dateOnly") {
|
|
57
|
-
|
|
58
|
-
.toPlainDate()
|
|
59
|
-
|
|
60
|
-
|
|
82
|
+
if (dateObj instanceof Temporal.ZonedDateTime) {
|
|
83
|
+
return dateObj.toPlainDate().toLocaleString(locale, {
|
|
84
|
+
dateStyle: (_a = format.dateFormat) !== null && _a !== void 0 ? _a : "medium",
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return dateObj.toLocaleString(locale, {
|
|
88
|
+
dateStyle: (_b = format.dateFormat) !== null && _b !== void 0 ? _b : "medium",
|
|
61
89
|
});
|
|
62
90
|
}
|
|
63
91
|
if ((format === null || format === void 0 ? void 0 : format.selectFormat) === "timeOnly") {
|
|
64
|
-
|
|
65
|
-
.toPlainTime()
|
|
66
|
-
|
|
67
|
-
|
|
92
|
+
if (dateObj instanceof Temporal.ZonedDateTime) {
|
|
93
|
+
return dateObj.toPlainTime().toLocaleString(locale, {
|
|
94
|
+
timeStyle: (_c = format.timeFormat) !== null && _c !== void 0 ? _c : "short",
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
return dateObj.toLocaleString(locale, {
|
|
98
|
+
timeStyle: (_d = format.timeFormat) !== null && _d !== void 0 ? _d : "short",
|
|
68
99
|
});
|
|
69
100
|
}
|
|
70
|
-
return
|
|
71
|
-
dateStyle: (
|
|
72
|
-
timeStyle: (
|
|
101
|
+
return dateObj.toLocaleString(locale, {
|
|
102
|
+
dateStyle: (_e = format === null || format === void 0 ? void 0 : format.dateFormat) !== null && _e !== void 0 ? _e : "medium",
|
|
103
|
+
timeStyle: (_f = format === null || format === void 0 ? void 0 : format.timeFormat) !== null && _f !== void 0 ? _f : "short",
|
|
73
104
|
});
|
|
74
105
|
};
|
|
75
106
|
/**
|
|
76
107
|
* Formats a date range.
|
|
77
108
|
*
|
|
78
|
-
* @param {
|
|
79
|
-
* @param {Date} end - The end date of the range.
|
|
109
|
+
* @param {DateRange} range - The start date of the range.
|
|
80
110
|
* @param {string} locale - The locale identifier (e.g., 'en-US', 'fr-FR') used for formatting.
|
|
81
111
|
* @param {Intl.DateTimeFormatOptions} [format] - Optional formatting options for the date and time.
|
|
82
112
|
* @returns {string} A formatted string representing the date range.
|
|
83
113
|
*/
|
|
84
|
-
const formatRangeUtil = (
|
|
114
|
+
const formatRangeUtil = (range, locale, format) => Intl.DateTimeFormat(locale, format).formatRange(range.start, range.end);
|
|
85
115
|
/**
|
|
86
116
|
* Subtracts a specified number of minutes from a date.
|
|
87
117
|
*
|
|
88
118
|
* @param {TemporalDate} date - The temporal date from which to subtract minutes.
|
|
89
119
|
* @param {number} amount - The number of minutes to subtract.
|
|
90
120
|
* @param {string} [timeZone] - The time zone to use for the subtraction. If not provided, the system's default time zone will be used.
|
|
91
|
-
* @returns {
|
|
121
|
+
* @returns {Temporal.ZonedDateTime} The resulting temporal date after subtracting the specified number of minutes.
|
|
92
122
|
* @example
|
|
93
123
|
* const dateString = "2023-05-10T10:30:00.000Z"
|
|
94
124
|
* const date = new Date(dateString);
|
|
@@ -105,7 +135,7 @@ const subtractMinutesUtil = (date, amount, timeZone) => {
|
|
|
105
135
|
* @param {TemporalDate} date - The temporal date from which to subtract hours.
|
|
106
136
|
* @param {number} amount - The number of hours to subtract.
|
|
107
137
|
* @param {string} [timeZone] - The time zone to use for the subtraction. If not provided, the system's default time zone will be used.
|
|
108
|
-
* @returns {
|
|
138
|
+
* @returns {Temporal.ZonedDateTime} The resulting temporal date after subtracting the specified number of hours.
|
|
109
139
|
* @example
|
|
110
140
|
* const dateString = "2023-05-10T10:30:00.000Z"
|
|
111
141
|
* const date = new Date(dateString);
|
|
@@ -122,7 +152,7 @@ const subtractHoursUtil = (date, amount, timeZone) => {
|
|
|
122
152
|
* @param {TemporalDate} date - The temporal date from which to subtract days.
|
|
123
153
|
* @param {number} amount - The number of days to subtract.
|
|
124
154
|
* @param {string} [timeZone] - The time zone to use for the subtraction. If not provided, the system's default time zone will be used.
|
|
125
|
-
* @returns {
|
|
155
|
+
* @returns {Temporal.ZonedDateTime} The resulting temporal date after subtracting the specified number of days.
|
|
126
156
|
* @example
|
|
127
157
|
* const dateString = "2023-05-10"
|
|
128
158
|
* const date = new Date(dateString);
|
|
@@ -139,7 +169,7 @@ const subtractDaysUtil = (date, amount, timeZone) => {
|
|
|
139
169
|
* @param {TemporalDate} date - The temporal date from which to subtract weeks.
|
|
140
170
|
* @param {number} amount - The number of weeks to subtract.
|
|
141
171
|
* @param {string} [timeZone] - The time zone to use for the subtraction. If not provided, the system's default time zone will be used.
|
|
142
|
-
* @returns {
|
|
172
|
+
* @returns {Temporal.ZonedDateTime} The resulting temporal date after subtracting the specified number of weeks.
|
|
143
173
|
* @example
|
|
144
174
|
* const dateString = "2023-05-10"
|
|
145
175
|
* const date = new Date(dateString);
|
|
@@ -156,7 +186,7 @@ const subtractWeeksUtil = (date, amount, timeZone) => {
|
|
|
156
186
|
* @param {TemporalDate} date - The temporal date from which to subtract months.
|
|
157
187
|
* @param {number} amount - The number of months to subtract.
|
|
158
188
|
* @param {string} [timeZone] - The time zone to use for the subtraction. If not provided, the system's default time zone will be used.
|
|
159
|
-
* @returns {
|
|
189
|
+
* @returns {Temporal.ZonedDateTime} The resulting temporal date after subtracting the specified number of months.
|
|
160
190
|
* @example
|
|
161
191
|
* const dateString = "2023-05-10"
|
|
162
192
|
* const date = new Date(dateString);
|
|
@@ -173,7 +203,7 @@ const subtractMonthsUtil = (date, amount, timeZone) => {
|
|
|
173
203
|
* @param {TemporalDate} date - The temporal date from which to subtract years.
|
|
174
204
|
* @param {number} amount - The number of years to subtract.
|
|
175
205
|
* @param {string} [timeZone] - The time zone to use for the subtraction. If not provided, the system's default time zone will be used.
|
|
176
|
-
* @returns {
|
|
206
|
+
* @returns {Temporal.ZonedDateTime} The resulting temporal date after subtracting the specified number of years.
|
|
177
207
|
* @example
|
|
178
208
|
* const dateString = "2023-05-10"
|
|
179
209
|
* const date = new Date(dateString);
|
|
@@ -190,7 +220,7 @@ const subtractYearsUtil = (date, amount, timeZone) => {
|
|
|
190
220
|
* @param {TemporalDate} date - The temporal date to which minutes will be added.
|
|
191
221
|
* @param {number} amount - The number of minutes to add.
|
|
192
222
|
* @param {string} [timeZone] - The time zone to use for the addition. If not provided, the system's default time zone will be used.
|
|
193
|
-
* @returns {
|
|
223
|
+
* @returns {Temporal.ZonedDateTime} The resulting temporal date after adding the specified number of minutes.
|
|
194
224
|
* @example
|
|
195
225
|
* const dateString = "2023-05-10"
|
|
196
226
|
* const date = new Date(dateString);
|
|
@@ -207,7 +237,7 @@ const addMinutesUtil = (date, amount, timeZone) => {
|
|
|
207
237
|
* @param {TemporalDate} date - The temporal date to which hours will be added.
|
|
208
238
|
* @param {number} amount - The number of hours to add.
|
|
209
239
|
* @param {string} [timeZone] - The time zone to use for the addition. If not provided, the system's default time zone will be used.
|
|
210
|
-
* @returns {
|
|
240
|
+
* @returns {Temporal.ZonedDateTime} The resulting temporal date after adding the specified number of hours.
|
|
211
241
|
* @example
|
|
212
242
|
* const dateString = "2023-05-10"
|
|
213
243
|
* const date = new Date(dateString);
|
|
@@ -224,7 +254,7 @@ const addHoursUtil = (date, amount, timeZone) => {
|
|
|
224
254
|
* @param {TemporalDate} date - The temporal date to which days will be added.
|
|
225
255
|
* @param {number} amount - The number of days to add.
|
|
226
256
|
* @param {string} [timeZone] - The time zone to use for the addition. If not provided, the system's default time zone will be used.
|
|
227
|
-
* @returns {
|
|
257
|
+
* @returns {Temporal.ZonedDateTime} The resulting temporal date after adding the specified number of days.
|
|
228
258
|
* @example
|
|
229
259
|
* const dateString = "2023-05-10"
|
|
230
260
|
* const date = new Date(dateString);
|
|
@@ -241,7 +271,7 @@ const addDaysUtil = (date, amount, timeZone) => {
|
|
|
241
271
|
* @param {TemporalDate} date - The temporal date to which weeks will be added.
|
|
242
272
|
* @param {number} amount - The number of weeks to add.
|
|
243
273
|
* @param {string} [timeZone] - The time zone to use for the addition. If not provided, the system's default time zone will be used.
|
|
244
|
-
* @returns {
|
|
274
|
+
* @returns {Temporal.ZonedDateTime} The resulting temporal date after adding the specified number of weeks.
|
|
245
275
|
* @example
|
|
246
276
|
* const dateString = "2023-05-10"
|
|
247
277
|
* const date = new Date(dateString);
|
|
@@ -258,7 +288,7 @@ const addWeeksUtil = (date, amount, timeZone) => {
|
|
|
258
288
|
* @param {TemporalDate} date - The temporal date to which months will be added.
|
|
259
289
|
* @param {number} amount - The number of months to add.
|
|
260
290
|
* @param {string} [timeZone] - The time zone to use for the addition. If not provided, the system's default time zone will be used.
|
|
261
|
-
* @returns {
|
|
291
|
+
* @returns {Temporal.ZonedDateTime} The resulting temporal date after adding the specified number of months.
|
|
262
292
|
* @example
|
|
263
293
|
* const dateString = "2023-05-10"
|
|
264
294
|
* const date = new Date(dateString);
|
|
@@ -275,7 +305,7 @@ const addMonthsUtil = (date, amount, timeZone) => {
|
|
|
275
305
|
* @param {TemporalDate} date - The temporal date to which years will be added.
|
|
276
306
|
* @param {number} amount - The number of years to add.
|
|
277
307
|
* @param {string} [timeZone] - The time zone to use for the addition. If not provided, the system's default time zone will be used.
|
|
278
|
-
* @returns {
|
|
308
|
+
* @returns {Temporal.ZonedDateTime} The resulting temporal date after adding the specified number of years.
|
|
279
309
|
* @example
|
|
280
310
|
* const dateString = "2023-05-10"
|
|
281
311
|
* const date = new Date(dateString);
|
|
@@ -291,7 +321,7 @@ const addYearsUtil = (date, amount, timeZone) => {
|
|
|
291
321
|
*
|
|
292
322
|
* @param {TemporalDate} date - The temporal date for which to determine the start of the minute.
|
|
293
323
|
* @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.
|
|
294
|
-
* @returns {
|
|
324
|
+
* @returns {Temporal.ZonedDateTime} The temporal date representing the start of the minute.
|
|
295
325
|
* @example
|
|
296
326
|
* const dateString = "2023-05-10T10:30:10.000Z"
|
|
297
327
|
* const date = new Date(dateString);
|
|
@@ -306,7 +336,7 @@ const startOfMinuteUtil = (date, timeZone) => {
|
|
|
306
336
|
*
|
|
307
337
|
* @param {TemporalDate} date - The temporal date for which to determine the end of the minute.
|
|
308
338
|
* @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.
|
|
309
|
-
* @returns {
|
|
339
|
+
* @returns {Temporal.ZonedDateTime} The temporal date representing the end of the minute.
|
|
310
340
|
* @example
|
|
311
341
|
* const dateString = "2023-05-10T10:30:10.000Z"
|
|
312
342
|
* const date = new Date(dateString);
|
|
@@ -324,7 +354,7 @@ const endOfMinuteUtil = (date, timeZone) => {
|
|
|
324
354
|
*
|
|
325
355
|
* @param {TemporalDate} date - The temporal date for which to determine the start of the hour.
|
|
326
356
|
* @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.
|
|
327
|
-
* @returns {
|
|
357
|
+
* @returns {Temporal.ZonedDateTime} The temporal date representing the start of the hour.
|
|
328
358
|
* @example
|
|
329
359
|
* const dateString = "2023-05-10T10:30:30.000Z"
|
|
330
360
|
* const date = new Date(dateString);
|
|
@@ -339,7 +369,7 @@ const startOfHourUtil = (date, timeZone) => {
|
|
|
339
369
|
*
|
|
340
370
|
* @param {TemporalDate} date - The temporal date for which to determine the end of the hour.
|
|
341
371
|
* @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.
|
|
342
|
-
* @returns {
|
|
372
|
+
* @returns {Temporal.ZonedDateTime} The temporal date representing the end of the hour.
|
|
343
373
|
* @example
|
|
344
374
|
* const dateString = "2023-05-10T10:30:00.000Z"
|
|
345
375
|
* const date = new Date(dateString);
|
|
@@ -357,7 +387,7 @@ const endOfHourUtil = (date, timeZone) => {
|
|
|
357
387
|
*
|
|
358
388
|
* @param {TemporalDate} date - The temporal date for which to determine the start of the day.
|
|
359
389
|
* @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.
|
|
360
|
-
* @returns {
|
|
390
|
+
* @returns {Temporal.ZonedDateTime} The temporal date representing the start of the day.
|
|
361
391
|
* @example
|
|
362
392
|
* const dateString = "2023-05-10T10:30:00.000Z"
|
|
363
393
|
* const date = new Date(dateString);
|
|
@@ -372,7 +402,7 @@ const startOfDayUtil = (date, timeZone) => {
|
|
|
372
402
|
*
|
|
373
403
|
* @param {TemporalDate} date - The temporal date for which to determine the end of the day.
|
|
374
404
|
* @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.
|
|
375
|
-
* @returns {
|
|
405
|
+
* @returns {Temporal.ZonedDateTime} The temporal date representing the end of the day.
|
|
376
406
|
* @example
|
|
377
407
|
* const dateString = "2023-05-10T10:30:00.000Z"
|
|
378
408
|
* const date = new Date(dateString);
|
|
@@ -380,9 +410,7 @@ const startOfDayUtil = (date, timeZone) => {
|
|
|
380
410
|
* 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" } }
|
|
381
411
|
*/
|
|
382
412
|
const endOfDayUtil = (date, timeZone) => {
|
|
383
|
-
const day = toZonedDateTimeUtil(date, timeZone)
|
|
384
|
-
.startOfDay()
|
|
385
|
-
.subtract(Temporal.Duration.from({ milliseconds: 1 }));
|
|
413
|
+
const day = toZonedDateTimeUtil(date, timeZone).startOfDay().subtract({ seconds: 1 });
|
|
386
414
|
return addDaysUtil(day, 1);
|
|
387
415
|
};
|
|
388
416
|
/**
|
|
@@ -390,7 +418,7 @@ const endOfDayUtil = (date, timeZone) => {
|
|
|
390
418
|
*
|
|
391
419
|
* @param {TemporalDate} date - The temporal date for which to determine the start of the week.
|
|
392
420
|
* @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.
|
|
393
|
-
* @returns {
|
|
421
|
+
* @returns {Temporal.ZonedDateTime} The temporal date representing the start of the week.
|
|
394
422
|
* @example
|
|
395
423
|
* const dateString = "2023-05-10T10:30:00.000Z"
|
|
396
424
|
* const date = new Date(dateString);
|
|
@@ -407,7 +435,7 @@ const startOfWeekUtil = (date, timeZone) => {
|
|
|
407
435
|
*
|
|
408
436
|
* @param {TemporalDate} date - The temporal date for which to determine the end of the week.
|
|
409
437
|
* @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.
|
|
410
|
-
* @returns {
|
|
438
|
+
* @returns {Temporal.ZonedDateTime} The temporal date representing the end of the week.
|
|
411
439
|
* @example
|
|
412
440
|
* const dateString = "2023-05-10T10:30:00.000Z"
|
|
413
441
|
* const date = new Date(dateString);
|
|
@@ -425,7 +453,7 @@ const endOfWeekUtil = (date, timeZone) => {
|
|
|
425
453
|
*
|
|
426
454
|
* @param {TemporalDate} date - The temporal date for which to determine the start of the month.
|
|
427
455
|
* @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.
|
|
428
|
-
* @returns {
|
|
456
|
+
* @returns {Temporal.ZonedDateTime} The temporal date representing the start of the month.
|
|
429
457
|
* @example
|
|
430
458
|
* const dateString = "2023-05-10T10:30:00.000Z"
|
|
431
459
|
* const date = new Date(dateString);
|
|
@@ -442,7 +470,7 @@ const startOfMonthUtil = (date, timeZone) => {
|
|
|
442
470
|
*
|
|
443
471
|
* @param {TemporalDate} date - The temporal date for which to determine the end of the month.
|
|
444
472
|
* @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.
|
|
445
|
-
* @returns {
|
|
473
|
+
* @returns {Temporal.ZonedDateTime} The temporal date representing the end of the month.
|
|
446
474
|
* @example
|
|
447
475
|
* const dateString = "2023-05-10"
|
|
448
476
|
* const date = new Date(dateString);
|
|
@@ -636,6 +664,22 @@ const isPastUtil = (date) => {
|
|
|
636
664
|
const isTodayUtil = (date) => {
|
|
637
665
|
return Temporal.PlainDate.compare(toZonedDateTimeUtil(Temporal.Now.instant()), toZonedDateTimeUtil(date)) === 0;
|
|
638
666
|
};
|
|
667
|
+
/**
|
|
668
|
+
*
|
|
669
|
+
*
|
|
670
|
+
* @param date - The date to check.
|
|
671
|
+
* @param start - The start date to compare against.
|
|
672
|
+
* @param end - The en date to compare against.
|
|
673
|
+
*/
|
|
674
|
+
const isBetweenUtil = (date, start, end) => {
|
|
675
|
+
if (!start || !end) {
|
|
676
|
+
throw new Error("Start and end dates must be provided");
|
|
677
|
+
}
|
|
678
|
+
const dateToCheck = new Date(date.toLocaleString());
|
|
679
|
+
const startDate = new Date(start.toLocaleString());
|
|
680
|
+
const endDate = new Date(end.toLocaleString());
|
|
681
|
+
return dateToCheck >= startDate && dateToCheck <= endDate;
|
|
682
|
+
};
|
|
639
683
|
/**
|
|
640
684
|
* Checks if a date is same day as current.
|
|
641
685
|
*
|
|
@@ -702,7 +746,9 @@ const isSameYearUtil = (date) => {
|
|
|
702
746
|
* Output: true
|
|
703
747
|
*/
|
|
704
748
|
const isEqualUtil = (from, to) => {
|
|
705
|
-
return toZonedDateTimeUtil(from)
|
|
749
|
+
return toZonedDateTimeUtil(from)
|
|
750
|
+
.round({ smallestUnit: "seconds" })
|
|
751
|
+
.equals(toZonedDateTimeUtil(to).round({ smallestUnit: "seconds" }));
|
|
706
752
|
};
|
|
707
753
|
/**
|
|
708
754
|
* Calculates the time duration between two TemporalDate objects and returns a human-readable string representation of the duration.
|
|
@@ -736,6 +782,9 @@ const timeSinceAuto = (from, to, timeZone, locale) => {
|
|
|
736
782
|
largestUnit: "years",
|
|
737
783
|
smallestUnit: "seconds",
|
|
738
784
|
}));
|
|
785
|
+
if (duration.blank) {
|
|
786
|
+
return getRelativeTimeFormat(locale).format(-1, "seconds");
|
|
787
|
+
}
|
|
739
788
|
if (duration.years !== 0) {
|
|
740
789
|
return timeSinceInYears(from, to, timeZone, locale);
|
|
741
790
|
}
|
|
@@ -862,6 +911,10 @@ const timeSinceInDays = (from, to, timeZone, locale) => {
|
|
|
862
911
|
* Output: "2 months ago"
|
|
863
912
|
*/
|
|
864
913
|
const timeSinceInMonths = (from, to, timeZone, locale) => {
|
|
914
|
+
const isLeapYear = toZonedDateTimeUtil(from, timeZone).inLeapYear && toZonedDateTimeUtil(to, timeZone).inLeapYear;
|
|
915
|
+
if (isLeapYear) {
|
|
916
|
+
return timeSinceInDays(from, to, timeZone, locale);
|
|
917
|
+
}
|
|
865
918
|
const duration = Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
|
|
866
919
|
largestUnit: "months",
|
|
867
920
|
}));
|
|
@@ -883,12 +936,75 @@ const timeSinceInMonths = (from, to, timeZone, locale) => {
|
|
|
883
936
|
* Output: "1 year ago"
|
|
884
937
|
*/
|
|
885
938
|
const timeSinceInYears = (from, to, timeZone, locale) => {
|
|
939
|
+
const isLeapYear = toZonedDateTimeUtil(from, timeZone).inLeapYear && toZonedDateTimeUtil(to, timeZone).inLeapYear;
|
|
940
|
+
if (isLeapYear) {
|
|
941
|
+
return timeSinceInDays(from, to, timeZone, locale);
|
|
942
|
+
}
|
|
886
943
|
const duration = Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
|
|
887
944
|
largestUnit: "years",
|
|
888
945
|
}));
|
|
889
946
|
return getRelativeTimeFormat(locale).format(duration.years, "years");
|
|
890
947
|
};
|
|
891
948
|
const getRelativeTimeFormat = (locale) => new Intl.RelativeTimeFormat(locale, { style: "long", numeric: "always" });
|
|
949
|
+
/**
|
|
950
|
+
* Get duration in human-readable form
|
|
951
|
+
*
|
|
952
|
+
* @param duration - years, months, days, minutes, seconds
|
|
953
|
+
* @param locale - selected locale
|
|
954
|
+
* @param format - long, short, narrow
|
|
955
|
+
* @returns {string} human-readable string
|
|
956
|
+
*/
|
|
957
|
+
const getDurationFormat = (duration, locale, format) => {
|
|
958
|
+
return new Intl.DurationFormat(locale, { style: format !== null && format !== void 0 ? format : "long" }).format(duration);
|
|
959
|
+
};
|
|
960
|
+
/**
|
|
961
|
+
* convert a number to a type
|
|
962
|
+
*
|
|
963
|
+
* @param {number} number - 230.45
|
|
964
|
+
* @param {TemporalTimeTypes} unit - "days | "hours" | minutes
|
|
965
|
+
* @param {Temporal.TimeUnit} roundAt - smallest unit to show
|
|
966
|
+
*/
|
|
967
|
+
const toDuration = (number, unit, roundAt) => {
|
|
968
|
+
switch (unit) {
|
|
969
|
+
case "days":
|
|
970
|
+
return Temporal.Duration.from(durationBuilder(number, "days")).round({
|
|
971
|
+
smallestUnit: roundAt !== null && roundAt !== void 0 ? roundAt : "seconds",
|
|
972
|
+
});
|
|
973
|
+
case "hours":
|
|
974
|
+
return Temporal.Duration.from(durationBuilder(number, "hours")).round({ smallestUnit: roundAt !== null && roundAt !== void 0 ? roundAt : "minutes" });
|
|
975
|
+
case "minutes":
|
|
976
|
+
return Temporal.Duration.from(durationBuilder(number, "minutes")).round({
|
|
977
|
+
smallestUnit: roundAt !== null && roundAt !== void 0 ? roundAt : "seconds",
|
|
978
|
+
});
|
|
979
|
+
default:
|
|
980
|
+
throw new Error("Not a valid unit", { cause: unit });
|
|
981
|
+
}
|
|
982
|
+
};
|
|
983
|
+
const durationBuilder = (number, unit) => {
|
|
984
|
+
const days = Math.floor(number);
|
|
985
|
+
const hours = unit === "hours" ? Math.floor(number) : Math.floor((number * 24) % 24);
|
|
986
|
+
const minutes = unit === "minutes" ? Math.floor(number) : Math.floor((number * 24 * 60) % 60);
|
|
987
|
+
const seconds = Math.floor((number * 24 * 60 * 60) % 60);
|
|
988
|
+
switch (unit) {
|
|
989
|
+
case "days":
|
|
990
|
+
if (Number.isSafeInteger(number)) {
|
|
991
|
+
return `P${number}D`;
|
|
992
|
+
}
|
|
993
|
+
return `P${days}DT${hours}H${minutes}M${seconds}S`;
|
|
994
|
+
case "hours":
|
|
995
|
+
if (Number.isSafeInteger(number)) {
|
|
996
|
+
return `PT${number}H`;
|
|
997
|
+
}
|
|
998
|
+
return `PT${hours}H${minutes}M${seconds}S`;
|
|
999
|
+
case "minutes":
|
|
1000
|
+
if (Number.isSafeInteger(number)) {
|
|
1001
|
+
return `PT${number}M`;
|
|
1002
|
+
}
|
|
1003
|
+
return `PT${minutes}M${seconds}S`;
|
|
1004
|
+
default:
|
|
1005
|
+
return `PT0S`;
|
|
1006
|
+
}
|
|
1007
|
+
};
|
|
892
1008
|
/**
|
|
893
1009
|
* Converts a JavaScript Date object to a Temporal Instant.
|
|
894
1010
|
*
|
|
@@ -900,6 +1016,9 @@ const getRelativeTimeFormat = (locale) => new Intl.RelativeTimeFormat(locale, {
|
|
|
900
1016
|
* Output: Temporal.Instant { year: ..., month: ..., day: ..., hour: ..., minute: ..., second: ..., millisecond: ... }
|
|
901
1017
|
*/
|
|
902
1018
|
const toTemporalUtil = (date) => {
|
|
1019
|
+
if (typeof date === "string") {
|
|
1020
|
+
return Temporal.Instant.from(date);
|
|
1021
|
+
}
|
|
903
1022
|
return Temporal.Instant.from(date.toISOString());
|
|
904
1023
|
};
|
|
905
1024
|
/**
|
|
@@ -918,6 +1037,12 @@ const toTemporalUtil = (date) => {
|
|
|
918
1037
|
* Output: Date { ... } (equivalent JavaScript Date object)
|
|
919
1038
|
*/
|
|
920
1039
|
const toDateUtil = (date) => {
|
|
1040
|
+
if (typeof date === "string" || typeof date === "number") {
|
|
1041
|
+
return new Date(date);
|
|
1042
|
+
}
|
|
1043
|
+
if (date instanceof Date) {
|
|
1044
|
+
return date;
|
|
1045
|
+
}
|
|
921
1046
|
if (date instanceof Temporal.ZonedDateTime) {
|
|
922
1047
|
date = toInstantUtil(date);
|
|
923
1048
|
}
|
|
@@ -934,6 +1059,12 @@ const toDateUtil = (date) => {
|
|
|
934
1059
|
* Output: Temporal.Instant { year: ..., month: ..., day: ..., hour: ..., minute: ..., second: ..., millisecond: ... }
|
|
935
1060
|
*/
|
|
936
1061
|
const toInstantUtil = (zonedDateTime) => {
|
|
1062
|
+
if (typeof zonedDateTime === "string") {
|
|
1063
|
+
return Temporal.Instant.from(zonedDateTime);
|
|
1064
|
+
}
|
|
1065
|
+
if (zonedDateTime instanceof Date) {
|
|
1066
|
+
return Temporal.Instant.from(zonedDateTime.toString());
|
|
1067
|
+
}
|
|
937
1068
|
return zonedDateTime.toInstant();
|
|
938
1069
|
};
|
|
939
1070
|
/**
|
|
@@ -954,7 +1085,7 @@ const toZonedDateTimeUtil = (date, timeZoneId, calendar) => {
|
|
|
954
1085
|
if (date instanceof Temporal.ZonedDateTime) {
|
|
955
1086
|
return date;
|
|
956
1087
|
}
|
|
957
|
-
if (date instanceof Date) {
|
|
1088
|
+
if (date instanceof Date || typeof date === "string") {
|
|
958
1089
|
date = toTemporalUtil(date);
|
|
959
1090
|
}
|
|
960
1091
|
return date.toZonedDateTime({
|
|
@@ -990,6 +1121,22 @@ const monthNameUtil = (date, format, locale) => {
|
|
|
990
1121
|
month: format,
|
|
991
1122
|
});
|
|
992
1123
|
};
|
|
1124
|
+
/**
|
|
1125
|
+
* Returns a list of month names
|
|
1126
|
+
*
|
|
1127
|
+
* @param {TemporalFormatStyle} format - select the format of the month name
|
|
1128
|
+
* @param {string} locale - The locale to use for formatting. If not provided, the system's default locale will be used.
|
|
1129
|
+
*/
|
|
1130
|
+
const monthsUtil = (format, locale) => {
|
|
1131
|
+
const months = [];
|
|
1132
|
+
const d = new Date();
|
|
1133
|
+
const monthsInYear = Temporal.PlainDate.from(d.toISOString()).monthsInYear;
|
|
1134
|
+
for (let month = 0; month < monthsInYear; ++month) {
|
|
1135
|
+
d.setMonth(month);
|
|
1136
|
+
months.push(d.toLocaleString(locale, { month: format !== null && format !== void 0 ? format : "long" }));
|
|
1137
|
+
}
|
|
1138
|
+
return months;
|
|
1139
|
+
};
|
|
993
1140
|
/**
|
|
994
1141
|
* Returns the day name for the given date.
|
|
995
1142
|
*
|
|
@@ -1018,5 +1165,21 @@ const dayNameUtil = (date, format, locale) => {
|
|
|
1018
1165
|
weekday: format,
|
|
1019
1166
|
});
|
|
1020
1167
|
};
|
|
1168
|
+
/**
|
|
1169
|
+
* Returns a list of day names
|
|
1170
|
+
*
|
|
1171
|
+
* @param {TemporalFormatStyle} format - select the format of the day name
|
|
1172
|
+
* @param {string} locale - The locale to use for formatting. If not provided, the system's default locale will be used.
|
|
1173
|
+
*/
|
|
1174
|
+
const daysUtil = (format, locale) => {
|
|
1175
|
+
const days = [];
|
|
1176
|
+
const d = new Date();
|
|
1177
|
+
const daysInWeek = Temporal.PlainDate.from(d.toISOString()).daysInWeek;
|
|
1178
|
+
for (let day = 0; day < daysInWeek; ++day) {
|
|
1179
|
+
d.setDate(day);
|
|
1180
|
+
days.push(d.toLocaleString(locale, { weekday: format !== null && format !== void 0 ? format : "long" }));
|
|
1181
|
+
}
|
|
1182
|
+
return days;
|
|
1183
|
+
};
|
|
1021
1184
|
|
|
1022
|
-
export { addDaysUtil, addHoursUtil, addMinutesUtil, addMonthsUtil, addWeeksUtil, addYearsUtil, dayNameUtil, differenceInDaysUtil, differenceInHoursUtil, differenceInMinutesUtil, differenceInMonthsUtil, differenceInWeeksUtil, endOfDayUtil, endOfHourUtil, endOfMinuteUtil, endOfMonthUtil, endOfWeekUtil, formatDateUtil, formatRangeUtil, isEqualUtil, isFutureUtil, isPastUtil, isSameDayUtil, isSameMonthUtil, isSameWeekUtil, isSameYearUtil, isTodayUtil, monthNameUtil, startOfDayUtil, startOfHourUtil, startOfMinuteUtil, startOfMonthUtil, startOfWeekUtil, subtractDaysUtil, subtractHoursUtil, subtractMinutesUtil, subtractMonthsUtil, subtractWeeksUtil, subtractYearsUtil, timeSinceAuto, timeSinceInDays, timeSinceInHours, timeSinceInMinutes, timeSinceInMonths, timeSinceInSeconds, timeSinceInYears, timeZonesAvailable, toDateUtil, toInstantUtil, toTemporalUtil, toZonedDateTimeUtil };
|
|
1185
|
+
export { addDaysUtil, addHoursUtil, addMinutesUtil, addMonthsUtil, addWeeksUtil, addYearsUtil, dayNameUtil, daysUtil, differenceInDaysUtil, differenceInHoursUtil, differenceInMinutesUtil, differenceInMonthsUtil, differenceInWeeksUtil, endOfDayUtil, endOfHourUtil, endOfMinuteUtil, endOfMonthUtil, endOfWeekUtil, formatDateUtil, formatRangeUtil, getDurationFormat, getTimeZone, getTimeZoneOffset, isBetweenUtil, isEqualUtil, isFutureUtil, isPastUtil, isSameDayUtil, isSameMonthUtil, isSameWeekUtil, isSameYearUtil, isTodayUtil, monthNameUtil, monthsUtil, startOfDayUtil, startOfHourUtil, startOfMinuteUtil, startOfMonthUtil, startOfWeekUtil, subtractDaysUtil, subtractHoursUtil, subtractMinutesUtil, subtractMonthsUtil, subtractWeeksUtil, subtractYearsUtil, timeSinceAuto, timeSinceInDays, timeSinceInHours, timeSinceInMinutes, timeSinceInMonths, timeSinceInSeconds, timeSinceInYears, timeZonesAvailable, toDateUtil, toDuration, toInstantUtil, toTemporalUtil, toZonedDateTimeUtil };
|
package/package.json
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trackunit/date-and-time-utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.28-alpha-26d41cfe4c9.0",
|
|
4
4
|
"repository": "https://github.com/Trackunit/manager",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
6
|
"engines": {
|
|
7
|
-
"node": ">=
|
|
7
|
+
"node": ">=20.x"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
10
|
"@formatjs/intl-enumerator": "^1.4.1",
|
|
11
|
-
"@js-temporal/polyfill": "^0.4.4"
|
|
11
|
+
"@js-temporal/polyfill": "^0.4.4",
|
|
12
|
+
"@formatjs/intl-durationformat": "^0.2.2"
|
|
12
13
|
},
|
|
13
14
|
"module": "./index.esm.js",
|
|
14
15
|
"main": "./index.cjs.js"
|