@trackunit/date-and-time-utils 0.0.48 → 0.0.49
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 +232 -9
- package/index.esm.js +225 -10
- package/package.json +3 -3
- package/src/DateAndTimeUtils.d.ts +148 -5
package/index.cjs.js
CHANGED
|
@@ -77,7 +77,7 @@ const timeZonesAvailable = !intlEnumerator.shouldPolyfill()
|
|
|
77
77
|
* const formattedDate = formatDateUtil(date, format, timeZone, locale);
|
|
78
78
|
* Output: "5/10/23, 12:00:00 AM EDT"
|
|
79
79
|
*/
|
|
80
|
-
const formatDateUtil = (date, format, timeZone, locale) => {
|
|
80
|
+
const formatDateUtil = (date, format, timeZone, locale, hourCycle) => {
|
|
81
81
|
var _a, _b, _c, _d, _e, _f;
|
|
82
82
|
const dateObj = timeZone ? toZonedDateTimeUtil(date, timeZone) : toTemporalUtil(toDateUtil(date));
|
|
83
83
|
if ((format === null || format === void 0 ? void 0 : format.selectFormat) === "dateOnly") {
|
|
@@ -88,21 +88,25 @@ const formatDateUtil = (date, format, timeZone, locale) => {
|
|
|
88
88
|
}
|
|
89
89
|
return dateObj.toLocaleString(locale, {
|
|
90
90
|
dateStyle: (_b = format.dateFormat) !== null && _b !== void 0 ? _b : "medium",
|
|
91
|
+
hourCycle: hourCycle !== null && hourCycle !== void 0 ? hourCycle : "h12",
|
|
91
92
|
});
|
|
92
93
|
}
|
|
93
94
|
if ((format === null || format === void 0 ? void 0 : format.selectFormat) === "timeOnly") {
|
|
94
95
|
if (dateObj instanceof polyfill.Temporal.ZonedDateTime) {
|
|
95
96
|
return dateObj.toPlainTime().toLocaleString(locale, {
|
|
96
97
|
timeStyle: (_c = format.timeFormat) !== null && _c !== void 0 ? _c : "short",
|
|
98
|
+
hourCycle: hourCycle !== null && hourCycle !== void 0 ? hourCycle : "h12",
|
|
97
99
|
});
|
|
98
100
|
}
|
|
99
101
|
return dateObj.toLocaleString(locale, {
|
|
100
102
|
timeStyle: (_d = format.timeFormat) !== null && _d !== void 0 ? _d : "short",
|
|
103
|
+
hourCycle: hourCycle !== null && hourCycle !== void 0 ? hourCycle : "h12",
|
|
101
104
|
});
|
|
102
105
|
}
|
|
103
106
|
return dateObj.toLocaleString(locale, {
|
|
104
107
|
dateStyle: (_e = format === null || format === void 0 ? void 0 : format.dateFormat) !== null && _e !== void 0 ? _e : "medium",
|
|
105
108
|
timeStyle: (_f = format === null || format === void 0 ? void 0 : format.timeFormat) !== null && _f !== void 0 ? _f : "short",
|
|
109
|
+
hourCycle: hourCycle !== null && hourCycle !== void 0 ? hourCycle : "h12",
|
|
106
110
|
});
|
|
107
111
|
};
|
|
108
112
|
/**
|
|
@@ -412,7 +416,7 @@ const startOfDayUtil = (date, timeZone) => {
|
|
|
412
416
|
* 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" } }
|
|
413
417
|
*/
|
|
414
418
|
const endOfDayUtil = (date, timeZone) => {
|
|
415
|
-
const day = toZonedDateTimeUtil(date, timeZone).startOfDay().subtract({
|
|
419
|
+
const day = toZonedDateTimeUtil(date, timeZone).startOfDay().subtract({ milliseconds: 1 });
|
|
416
420
|
return addDaysUtil(day, 1);
|
|
417
421
|
};
|
|
418
422
|
/**
|
|
@@ -442,10 +446,10 @@ const startOfWeekUtil = (date, timeZone) => {
|
|
|
442
446
|
* const dateString = "2023-05-10T10:30:00.000Z"
|
|
443
447
|
* const date = new Date(dateString);
|
|
444
448
|
* const result = startOfWeekUtil(date);
|
|
445
|
-
* Output: Temporal.ZonedDateTime { year: 2023, month: 5, day: 14, hour:
|
|
449
|
+
* Output: Temporal.ZonedDateTime { year: 2023, month: 5, day: 14, hour: 23, minute: 59, second: 59, millisecond: 999, microsecond: 0, nanosecond: 0, timeZone: Temporal.TimeZone { id: "America/New_York" }, calendar: Temporal.Calendar { id: "iso8601" } }
|
|
446
450
|
*/
|
|
447
451
|
const endOfWeekUtil = (date, timeZone) => {
|
|
448
|
-
const day = toZonedDateTimeUtil(date, timeZone)
|
|
452
|
+
const day = endOfDayUtil(toZonedDateTimeUtil(date, timeZone));
|
|
449
453
|
const currentDay = day.dayOfWeek;
|
|
450
454
|
const totalDays = day.daysInWeek;
|
|
451
455
|
return addDaysUtil(day, totalDays - currentDay);
|
|
@@ -477,14 +481,47 @@ const startOfMonthUtil = (date, timeZone) => {
|
|
|
477
481
|
* const dateString = "2023-05-10"
|
|
478
482
|
* const date = new Date(dateString);
|
|
479
483
|
* const result = endOfMonthUtil(date);
|
|
480
|
-
* Output: Temporal.ZonedDateTime { year: 2023, month: 5, day: 31, hour:
|
|
484
|
+
* Output: Temporal.ZonedDateTime { year: 2023, month: 5, day: 31, hour: 23, minute: 59, second: 59, millisecond: 999, microsecond: 0, nanosecond: 0, timeZone: Temporal.TimeZone { id: "America/New_York" }, calendar: Temporal.Calendar { id: "iso8601" } }
|
|
481
485
|
*/
|
|
482
486
|
const endOfMonthUtil = (date, timeZone) => {
|
|
483
|
-
const day = toZonedDateTimeUtil(date, timeZone)
|
|
487
|
+
const day = endOfDayUtil(toZonedDateTimeUtil(date, timeZone));
|
|
484
488
|
const currentDay = day.day;
|
|
485
489
|
const totalDays = day.daysInMonth;
|
|
486
490
|
return addDaysUtil(day, totalDays - currentDay);
|
|
487
491
|
};
|
|
492
|
+
/**
|
|
493
|
+
* Calculates the difference in seconds between two dates.
|
|
494
|
+
*
|
|
495
|
+
* @param {TemporalDate} from - The starting date.
|
|
496
|
+
* @param {TemporalDate} to - The ending date.
|
|
497
|
+
* @returns {number} The difference in seconds between the two dates.
|
|
498
|
+
* @example
|
|
499
|
+
* const dateString = "2023-05-10T10:30:00.000Z"
|
|
500
|
+
* const fromDate = new Date(dateString);
|
|
501
|
+
* const toDate = new Date(dateString);
|
|
502
|
+
* const result = differenceInSecondsUtil(fromDate, toDate);
|
|
503
|
+
* Output: 0 (if fromDate and toDate are the same)
|
|
504
|
+
* @example
|
|
505
|
+
* const dateString1 = "2023-05-10T10:30:00.000Z"
|
|
506
|
+
* const dateString2 = "2023-05-10T10:35:00.000Z"
|
|
507
|
+
* const fromDate = new Date(dateString1);
|
|
508
|
+
* const toDate = new Date(dateString2);
|
|
509
|
+
* const result = differenceInSecondsUtil(fromDate, toDate);
|
|
510
|
+
* Output: 5
|
|
511
|
+
*/
|
|
512
|
+
const differenceInSecondsUtil = (from, to) => {
|
|
513
|
+
let until = new polyfill.Temporal.Duration();
|
|
514
|
+
try {
|
|
515
|
+
until = toZonedDateTimeUtil(from).toPlainTime().until(toZonedDateTimeUtil(to).toPlainTime(), {
|
|
516
|
+
largestUnit: "seconds",
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
catch (error) {
|
|
520
|
+
// eslint-disable-next-line no-console
|
|
521
|
+
console.warn("Error: ", error);
|
|
522
|
+
}
|
|
523
|
+
return until.seconds;
|
|
524
|
+
};
|
|
488
525
|
/**
|
|
489
526
|
* Calculates the difference in minutes between two dates.
|
|
490
527
|
*
|
|
@@ -620,6 +657,40 @@ const differenceInWeeksUtil = (from, to) => {
|
|
|
620
657
|
}
|
|
621
658
|
return until.weeks;
|
|
622
659
|
};
|
|
660
|
+
/**
|
|
661
|
+
* Calculates the difference in years between two dates.
|
|
662
|
+
*
|
|
663
|
+
* @param {TemporalDate} from - The starting date.
|
|
664
|
+
* @param {TemporalDate} to - The ending date.
|
|
665
|
+
* @returns {number} The difference in years between the two dates.
|
|
666
|
+
* @example
|
|
667
|
+
* const dateString1 = "2023-05-10"
|
|
668
|
+
* const dateString2 = "2023-05-12"
|
|
669
|
+
* const fromDate = new Date(dateString1);
|
|
670
|
+
* const toDate = new Date(dateString2);
|
|
671
|
+
* const result = differenceInYearsUtil(fromDate, toDate);
|
|
672
|
+
* Output: 0 (if fromDate and toDate are in the same year)
|
|
673
|
+
* @example
|
|
674
|
+
* const dateString1 = "2023-05-10"
|
|
675
|
+
* const dateString2 = "2025-05-18"
|
|
676
|
+
* const fromDate = new Date(dateString1);
|
|
677
|
+
* const toDate = new Date(dateString2);
|
|
678
|
+
* const result = differenceInWeeksUtil(fromDate, toDate);
|
|
679
|
+
* Output: 2
|
|
680
|
+
*/
|
|
681
|
+
const differenceInYearsUtil = (from, to) => {
|
|
682
|
+
let until = new polyfill.Temporal.Duration();
|
|
683
|
+
try {
|
|
684
|
+
until = toZonedDateTimeUtil(from).toPlainDate().until(toZonedDateTimeUtil(to).toPlainDate(), {
|
|
685
|
+
largestUnit: "year",
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
catch (error) {
|
|
689
|
+
// eslint-disable-next-line no-console
|
|
690
|
+
console.warn("Error: ", error);
|
|
691
|
+
}
|
|
692
|
+
return until.years;
|
|
693
|
+
};
|
|
623
694
|
/**
|
|
624
695
|
* Calculates the difference in months between two dates.
|
|
625
696
|
*
|
|
@@ -1064,7 +1135,7 @@ const getDurationFormat = (duration, locale, format) => {
|
|
|
1064
1135
|
* convert a number to a type
|
|
1065
1136
|
*
|
|
1066
1137
|
* @param {number} number - 230.45
|
|
1067
|
-
* @param {TemporalTimeTypes} unit - "days | "hours" | minutes
|
|
1138
|
+
* @param {TemporalTimeTypes} unit - "days | "hours" | "minutes" | "seconds"
|
|
1068
1139
|
* @param {Temporal.TimeUnit} roundAt - smallest unit to show
|
|
1069
1140
|
*/
|
|
1070
1141
|
const toDuration = (number, unit, roundAt) => {
|
|
@@ -1079,6 +1150,8 @@ const toDuration = (number, unit, roundAt) => {
|
|
|
1079
1150
|
return polyfill.Temporal.Duration.from(durationBuilder(number, "minutes")).round({
|
|
1080
1151
|
smallestUnit: roundAt !== null && roundAt !== void 0 ? roundAt : "seconds",
|
|
1081
1152
|
});
|
|
1153
|
+
case "seconds":
|
|
1154
|
+
return polyfill.Temporal.Duration.from(durationBuilder(number, "seconds")).round({ smallestUnit: roundAt !== null && roundAt !== void 0 ? roundAt : "seconds" });
|
|
1082
1155
|
default:
|
|
1083
1156
|
throw new Error("Not a valid unit", { cause: unit });
|
|
1084
1157
|
}
|
|
@@ -1088,7 +1161,7 @@ const defaultDuration = { days: 0, hours: 0, minutes: 0, seconds: 0 };
|
|
|
1088
1161
|
* Build a duration string
|
|
1089
1162
|
*
|
|
1090
1163
|
* @param number - number to convert
|
|
1091
|
-
* @param unit - "days | "hours" | minutes
|
|
1164
|
+
* @param unit - "days | "hours" | "minutes" | "seconds"
|
|
1092
1165
|
*/
|
|
1093
1166
|
const durationBuilder = (number, unit) => {
|
|
1094
1167
|
const isSigned = Math.sign(number) === -1;
|
|
@@ -1112,6 +1185,11 @@ const durationBuilder = (number, unit) => {
|
|
|
1112
1185
|
return `${sign}PT${unsignedNumber}M`;
|
|
1113
1186
|
}
|
|
1114
1187
|
return `${sign}PT${minutes}M${seconds}S`;
|
|
1188
|
+
case "seconds":
|
|
1189
|
+
if (isInt) {
|
|
1190
|
+
return `${sign}PT${unsignedNumber}S`;
|
|
1191
|
+
}
|
|
1192
|
+
return `${sign}PT${seconds}S`;
|
|
1115
1193
|
default:
|
|
1116
1194
|
return `PT0S`;
|
|
1117
1195
|
}
|
|
@@ -1120,7 +1198,7 @@ const durationBuilder = (number, unit) => {
|
|
|
1120
1198
|
* Calculate time in days, hours, minutes, and seconds
|
|
1121
1199
|
*
|
|
1122
1200
|
* @param {number} number - number to convert
|
|
1123
|
-
* @param {TemporalTimeTypes} unit - "days | "hours" | minutes
|
|
1201
|
+
* @param {TemporalTimeTypes} unit - "days | "hours" | "minutes" | "seconds"
|
|
1124
1202
|
*/
|
|
1125
1203
|
const calculateTime = (number, unit) => {
|
|
1126
1204
|
switch (unit) {
|
|
@@ -1365,6 +1443,143 @@ const daysUtil = (format, locale) => {
|
|
|
1365
1443
|
}
|
|
1366
1444
|
return days;
|
|
1367
1445
|
};
|
|
1446
|
+
/**
|
|
1447
|
+
* Returns the UTC date-time for the given time zone.
|
|
1448
|
+
*
|
|
1449
|
+
* @param {Date} from - The date to convert to UTC.
|
|
1450
|
+
* @param {string} timeZoneId - The time zone of the input date.
|
|
1451
|
+
*/
|
|
1452
|
+
const getUTCFromTimeZonedUtil = (from, timeZoneId) => {
|
|
1453
|
+
const zonedDateTime = toZonedDateTimeUtil(from, timeZoneId);
|
|
1454
|
+
return toDateUtil(zonedDateTime.toInstant().toZonedDateTimeISO("UTC"));
|
|
1455
|
+
};
|
|
1456
|
+
/**
|
|
1457
|
+
* @name getWeekUtil
|
|
1458
|
+
* @summary Get the local week index of the given date.
|
|
1459
|
+
* @description
|
|
1460
|
+
* Get the local week index of the given date using Temporal.
|
|
1461
|
+
* The exact calculation depends on the values of `weekStartsOn` (the index of the first day of the week)
|
|
1462
|
+
* and `firstWeekContainsDate` (the day in January, which is always in the first week of the week-numbering year).
|
|
1463
|
+
* @param {Temporal.PlainDate | Date | string} date - The given date (can be either Temporal.PlainDate, a native Date, or an ISO date string).
|
|
1464
|
+
* @param {GetWeekUtilOptions} [options] - Optional settings for the week calculation.
|
|
1465
|
+
* @returns {number} - The week number of the year.
|
|
1466
|
+
* @example
|
|
1467
|
+
* // Which week of the local week numbering year is 2 January 2005 with default options?
|
|
1468
|
+
* const result = getWeekUtil(new Date(2005, 0, 2))
|
|
1469
|
+
* //=> 2
|
|
1470
|
+
* @example
|
|
1471
|
+
* // Which week of the local week numbering year is 2 January 2005, if Monday is the first day of the week,
|
|
1472
|
+
* // and the first week of the year always contains 4 January?
|
|
1473
|
+
* const result = getWeekUtil("2005-01-02T00:00:00.000Z", {
|
|
1474
|
+
* weekStartsOn: 1,
|
|
1475
|
+
* firstWeekContainsDate: 4
|
|
1476
|
+
* })
|
|
1477
|
+
* //=> 53
|
|
1478
|
+
*/
|
|
1479
|
+
const getWeekUtil = (date, options) => {
|
|
1480
|
+
const _date = convertToPlainDate(date);
|
|
1481
|
+
const { weekStartsOn = 0, firstWeekContainsDate = 1 } = options || {};
|
|
1482
|
+
const startOfCurrentWeek = startOfWeek(_date, weekStartsOn);
|
|
1483
|
+
const startOfWeekYear = startOfWeek(polyfill.Temporal.PlainDate.from({
|
|
1484
|
+
year: _date.year,
|
|
1485
|
+
month: 1,
|
|
1486
|
+
day: firstWeekContainsDate,
|
|
1487
|
+
}), weekStartsOn);
|
|
1488
|
+
const diffInDays = startOfCurrentWeek.since(startOfWeekYear).total({ unit: "days" });
|
|
1489
|
+
return Math.floor(diffInDays / 7) + 1;
|
|
1490
|
+
};
|
|
1491
|
+
/**
|
|
1492
|
+
* @name convertMillisecondsUtil
|
|
1493
|
+
* @summary Convert milliseconds using Temporal.
|
|
1494
|
+
* @description
|
|
1495
|
+
* Convert milliseconds to the specified unit using Temporal.
|
|
1496
|
+
* @param {number} milliseconds - The number of milliseconds to convert.
|
|
1497
|
+
* @returns {number} - The converted value.
|
|
1498
|
+
* @example
|
|
1499
|
+
* const result = convertMillisecondsUtil(60000, "minute")
|
|
1500
|
+
* //=> 1
|
|
1501
|
+
* @example
|
|
1502
|
+
* const result = convertMillisecondsUtil(120000, "second")
|
|
1503
|
+
* //=> 120
|
|
1504
|
+
* @example
|
|
1505
|
+
* const result = convertMillisecondsUtil(3600000, "hour")
|
|
1506
|
+
* //=> 1
|
|
1507
|
+
*/
|
|
1508
|
+
const convertMillisecondsUtil = (milliseconds, unit) => {
|
|
1509
|
+
return polyfill.Temporal.Duration.from({ milliseconds }).total({ unit });
|
|
1510
|
+
};
|
|
1511
|
+
/**
|
|
1512
|
+
* @name convertSecondsUtil
|
|
1513
|
+
* @summary Convert seconds using Temporal.
|
|
1514
|
+
* @description
|
|
1515
|
+
* Convert seconds to the specified unit using Temporal.
|
|
1516
|
+
* @param {number} seconds - The number of seconds to convert.
|
|
1517
|
+
* @returns {number} - The converted value.
|
|
1518
|
+
* @example
|
|
1519
|
+
* const result = convertSecondsUtil(60, "minute")
|
|
1520
|
+
* //=> 1
|
|
1521
|
+
* @example
|
|
1522
|
+
* const result = convertSecondsUtil(120, "second")
|
|
1523
|
+
* //=> 120
|
|
1524
|
+
*/
|
|
1525
|
+
const convertSecondsUtil = (seconds, unit) => {
|
|
1526
|
+
return polyfill.Temporal.Duration.from({ seconds }).total({ unit });
|
|
1527
|
+
};
|
|
1528
|
+
/**
|
|
1529
|
+
* @name convertMinutesUtil
|
|
1530
|
+
* @summary Convert seconds using Temporal.
|
|
1531
|
+
* @description
|
|
1532
|
+
* Convert minutes to the specified unit using Temporal.
|
|
1533
|
+
* @param {number} minutes - The number of minutes to convert.
|
|
1534
|
+
* @returns {number} - The converted value.
|
|
1535
|
+
* @example
|
|
1536
|
+
* const result = convertMinutesUtil(60, "minute")
|
|
1537
|
+
* //=> 1
|
|
1538
|
+
* @example
|
|
1539
|
+
* const result = convertMinutesUtil(120, "second")
|
|
1540
|
+
* //=> 120
|
|
1541
|
+
*/
|
|
1542
|
+
const convertMinutesUtil = (minutes, unit) => {
|
|
1543
|
+
return polyfill.Temporal.Duration.from({ minutes }).total({ unit });
|
|
1544
|
+
};
|
|
1545
|
+
/**
|
|
1546
|
+
* @name convertHoursUtil
|
|
1547
|
+
* @summary Convert hours using Temporal.
|
|
1548
|
+
* @description
|
|
1549
|
+
* Convert hours to the specified unit using Temporal.
|
|
1550
|
+
* @param {number} hours - The number of hours to convert.
|
|
1551
|
+
* @returns {number} - The converted value.
|
|
1552
|
+
* @example
|
|
1553
|
+
* const result = convertHoursUtil(24, "day")
|
|
1554
|
+
* //=> 1
|
|
1555
|
+
* @example
|
|
1556
|
+
* const result = convertHoursUtil(48, "hour")
|
|
1557
|
+
* //=> 48
|
|
1558
|
+
*/
|
|
1559
|
+
const convertHoursUtil = (hours, unit) => {
|
|
1560
|
+
return polyfill.Temporal.Duration.from({ hours }).total({ unit });
|
|
1561
|
+
};
|
|
1562
|
+
const convertToPlainDate = (date) => {
|
|
1563
|
+
if (date instanceof polyfill.Temporal.PlainDate) {
|
|
1564
|
+
return date;
|
|
1565
|
+
}
|
|
1566
|
+
if (typeof date === "string") {
|
|
1567
|
+
return polyfill.Temporal.PlainDate.from(date);
|
|
1568
|
+
}
|
|
1569
|
+
if (date instanceof Date) {
|
|
1570
|
+
const dateString = date.toISOString().split("T")[0];
|
|
1571
|
+
if (!dateString) {
|
|
1572
|
+
throw new Error("Invalid Date object. Cannot convert to Temporal.PlainDate.");
|
|
1573
|
+
}
|
|
1574
|
+
return polyfill.Temporal.PlainDate.from(dateString);
|
|
1575
|
+
}
|
|
1576
|
+
throw new Error("Unsupported date format. Please provide a Temporal.PlainDate, Date, or ISO string.");
|
|
1577
|
+
};
|
|
1578
|
+
const startOfWeek = (date, weekStartsOn) => {
|
|
1579
|
+
const dayOfWeek = date.dayOfWeek;
|
|
1580
|
+
const diff = (dayOfWeek - weekStartsOn + 7) % 7;
|
|
1581
|
+
return date.subtract({ days: diff });
|
|
1582
|
+
};
|
|
1368
1583
|
|
|
1369
1584
|
exports.addDaysUtil = addDaysUtil;
|
|
1370
1585
|
exports.addHoursUtil = addHoursUtil;
|
|
@@ -1372,13 +1587,19 @@ exports.addMinutesUtil = addMinutesUtil;
|
|
|
1372
1587
|
exports.addMonthsUtil = addMonthsUtil;
|
|
1373
1588
|
exports.addWeeksUtil = addWeeksUtil;
|
|
1374
1589
|
exports.addYearsUtil = addYearsUtil;
|
|
1590
|
+
exports.convertHoursUtil = convertHoursUtil;
|
|
1591
|
+
exports.convertMillisecondsUtil = convertMillisecondsUtil;
|
|
1592
|
+
exports.convertMinutesUtil = convertMinutesUtil;
|
|
1593
|
+
exports.convertSecondsUtil = convertSecondsUtil;
|
|
1375
1594
|
exports.dayNameUtil = dayNameUtil;
|
|
1376
1595
|
exports.daysUtil = daysUtil;
|
|
1377
1596
|
exports.differenceInDaysUtil = differenceInDaysUtil;
|
|
1378
1597
|
exports.differenceInHoursUtil = differenceInHoursUtil;
|
|
1379
1598
|
exports.differenceInMinutesUtil = differenceInMinutesUtil;
|
|
1380
1599
|
exports.differenceInMonthsUtil = differenceInMonthsUtil;
|
|
1600
|
+
exports.differenceInSecondsUtil = differenceInSecondsUtil;
|
|
1381
1601
|
exports.differenceInWeeksUtil = differenceInWeeksUtil;
|
|
1602
|
+
exports.differenceInYearsUtil = differenceInYearsUtil;
|
|
1382
1603
|
exports.endOfDayUtil = endOfDayUtil;
|
|
1383
1604
|
exports.endOfHourUtil = endOfHourUtil;
|
|
1384
1605
|
exports.endOfMinuteUtil = endOfMinuteUtil;
|
|
@@ -1389,6 +1610,8 @@ exports.formatRangeUtil = formatRangeUtil;
|
|
|
1389
1610
|
exports.getDurationFormat = getDurationFormat;
|
|
1390
1611
|
exports.getTimeZone = getTimeZone;
|
|
1391
1612
|
exports.getTimeZoneOffset = getTimeZoneOffset;
|
|
1613
|
+
exports.getUTCFromTimeZonedUtil = getUTCFromTimeZonedUtil;
|
|
1614
|
+
exports.getWeekUtil = getWeekUtil;
|
|
1392
1615
|
exports.isBetweenUtil = isBetweenUtil;
|
|
1393
1616
|
exports.isEqualUtil = isEqualUtil;
|
|
1394
1617
|
exports.isFutureUtil = isFutureUtil;
|
package/index.esm.js
CHANGED
|
@@ -75,7 +75,7 @@ const timeZonesAvailable = !shouldPolyfill()
|
|
|
75
75
|
* const formattedDate = formatDateUtil(date, format, timeZone, locale);
|
|
76
76
|
* Output: "5/10/23, 12:00:00 AM EDT"
|
|
77
77
|
*/
|
|
78
|
-
const formatDateUtil = (date, format, timeZone, locale) => {
|
|
78
|
+
const formatDateUtil = (date, format, timeZone, locale, hourCycle) => {
|
|
79
79
|
var _a, _b, _c, _d, _e, _f;
|
|
80
80
|
const dateObj = timeZone ? toZonedDateTimeUtil(date, timeZone) : toTemporalUtil(toDateUtil(date));
|
|
81
81
|
if ((format === null || format === void 0 ? void 0 : format.selectFormat) === "dateOnly") {
|
|
@@ -86,21 +86,25 @@ const formatDateUtil = (date, format, timeZone, locale) => {
|
|
|
86
86
|
}
|
|
87
87
|
return dateObj.toLocaleString(locale, {
|
|
88
88
|
dateStyle: (_b = format.dateFormat) !== null && _b !== void 0 ? _b : "medium",
|
|
89
|
+
hourCycle: hourCycle !== null && hourCycle !== void 0 ? hourCycle : "h12",
|
|
89
90
|
});
|
|
90
91
|
}
|
|
91
92
|
if ((format === null || format === void 0 ? void 0 : format.selectFormat) === "timeOnly") {
|
|
92
93
|
if (dateObj instanceof Temporal.ZonedDateTime) {
|
|
93
94
|
return dateObj.toPlainTime().toLocaleString(locale, {
|
|
94
95
|
timeStyle: (_c = format.timeFormat) !== null && _c !== void 0 ? _c : "short",
|
|
96
|
+
hourCycle: hourCycle !== null && hourCycle !== void 0 ? hourCycle : "h12",
|
|
95
97
|
});
|
|
96
98
|
}
|
|
97
99
|
return dateObj.toLocaleString(locale, {
|
|
98
100
|
timeStyle: (_d = format.timeFormat) !== null && _d !== void 0 ? _d : "short",
|
|
101
|
+
hourCycle: hourCycle !== null && hourCycle !== void 0 ? hourCycle : "h12",
|
|
99
102
|
});
|
|
100
103
|
}
|
|
101
104
|
return dateObj.toLocaleString(locale, {
|
|
102
105
|
dateStyle: (_e = format === null || format === void 0 ? void 0 : format.dateFormat) !== null && _e !== void 0 ? _e : "medium",
|
|
103
106
|
timeStyle: (_f = format === null || format === void 0 ? void 0 : format.timeFormat) !== null && _f !== void 0 ? _f : "short",
|
|
107
|
+
hourCycle: hourCycle !== null && hourCycle !== void 0 ? hourCycle : "h12",
|
|
104
108
|
});
|
|
105
109
|
};
|
|
106
110
|
/**
|
|
@@ -410,7 +414,7 @@ const startOfDayUtil = (date, timeZone) => {
|
|
|
410
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" } }
|
|
411
415
|
*/
|
|
412
416
|
const endOfDayUtil = (date, timeZone) => {
|
|
413
|
-
const day = toZonedDateTimeUtil(date, timeZone).startOfDay().subtract({
|
|
417
|
+
const day = toZonedDateTimeUtil(date, timeZone).startOfDay().subtract({ milliseconds: 1 });
|
|
414
418
|
return addDaysUtil(day, 1);
|
|
415
419
|
};
|
|
416
420
|
/**
|
|
@@ -440,10 +444,10 @@ const startOfWeekUtil = (date, timeZone) => {
|
|
|
440
444
|
* const dateString = "2023-05-10T10:30:00.000Z"
|
|
441
445
|
* const date = new Date(dateString);
|
|
442
446
|
* const result = startOfWeekUtil(date);
|
|
443
|
-
* Output: Temporal.ZonedDateTime { year: 2023, month: 5, day: 14, hour:
|
|
447
|
+
* Output: Temporal.ZonedDateTime { year: 2023, month: 5, day: 14, hour: 23, minute: 59, second: 59, millisecond: 999, microsecond: 0, nanosecond: 0, timeZone: Temporal.TimeZone { id: "America/New_York" }, calendar: Temporal.Calendar { id: "iso8601" } }
|
|
444
448
|
*/
|
|
445
449
|
const endOfWeekUtil = (date, timeZone) => {
|
|
446
|
-
const day = toZonedDateTimeUtil(date, timeZone)
|
|
450
|
+
const day = endOfDayUtil(toZonedDateTimeUtil(date, timeZone));
|
|
447
451
|
const currentDay = day.dayOfWeek;
|
|
448
452
|
const totalDays = day.daysInWeek;
|
|
449
453
|
return addDaysUtil(day, totalDays - currentDay);
|
|
@@ -475,14 +479,47 @@ const startOfMonthUtil = (date, timeZone) => {
|
|
|
475
479
|
* const dateString = "2023-05-10"
|
|
476
480
|
* const date = new Date(dateString);
|
|
477
481
|
* const result = endOfMonthUtil(date);
|
|
478
|
-
* Output: Temporal.ZonedDateTime { year: 2023, month: 5, day: 31, hour:
|
|
482
|
+
* Output: Temporal.ZonedDateTime { year: 2023, month: 5, day: 31, hour: 23, minute: 59, second: 59, millisecond: 999, microsecond: 0, nanosecond: 0, timeZone: Temporal.TimeZone { id: "America/New_York" }, calendar: Temporal.Calendar { id: "iso8601" } }
|
|
479
483
|
*/
|
|
480
484
|
const endOfMonthUtil = (date, timeZone) => {
|
|
481
|
-
const day = toZonedDateTimeUtil(date, timeZone)
|
|
485
|
+
const day = endOfDayUtil(toZonedDateTimeUtil(date, timeZone));
|
|
482
486
|
const currentDay = day.day;
|
|
483
487
|
const totalDays = day.daysInMonth;
|
|
484
488
|
return addDaysUtil(day, totalDays - currentDay);
|
|
485
489
|
};
|
|
490
|
+
/**
|
|
491
|
+
* Calculates the difference in seconds between two dates.
|
|
492
|
+
*
|
|
493
|
+
* @param {TemporalDate} from - The starting date.
|
|
494
|
+
* @param {TemporalDate} to - The ending date.
|
|
495
|
+
* @returns {number} The difference in seconds between the two dates.
|
|
496
|
+
* @example
|
|
497
|
+
* const dateString = "2023-05-10T10:30:00.000Z"
|
|
498
|
+
* const fromDate = new Date(dateString);
|
|
499
|
+
* const toDate = new Date(dateString);
|
|
500
|
+
* const result = differenceInSecondsUtil(fromDate, toDate);
|
|
501
|
+
* Output: 0 (if fromDate and toDate are the same)
|
|
502
|
+
* @example
|
|
503
|
+
* const dateString1 = "2023-05-10T10:30:00.000Z"
|
|
504
|
+
* const dateString2 = "2023-05-10T10:35:00.000Z"
|
|
505
|
+
* const fromDate = new Date(dateString1);
|
|
506
|
+
* const toDate = new Date(dateString2);
|
|
507
|
+
* const result = differenceInSecondsUtil(fromDate, toDate);
|
|
508
|
+
* Output: 5
|
|
509
|
+
*/
|
|
510
|
+
const differenceInSecondsUtil = (from, to) => {
|
|
511
|
+
let until = new Temporal.Duration();
|
|
512
|
+
try {
|
|
513
|
+
until = toZonedDateTimeUtil(from).toPlainTime().until(toZonedDateTimeUtil(to).toPlainTime(), {
|
|
514
|
+
largestUnit: "seconds",
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
catch (error) {
|
|
518
|
+
// eslint-disable-next-line no-console
|
|
519
|
+
console.warn("Error: ", error);
|
|
520
|
+
}
|
|
521
|
+
return until.seconds;
|
|
522
|
+
};
|
|
486
523
|
/**
|
|
487
524
|
* Calculates the difference in minutes between two dates.
|
|
488
525
|
*
|
|
@@ -618,6 +655,40 @@ const differenceInWeeksUtil = (from, to) => {
|
|
|
618
655
|
}
|
|
619
656
|
return until.weeks;
|
|
620
657
|
};
|
|
658
|
+
/**
|
|
659
|
+
* Calculates the difference in years between two dates.
|
|
660
|
+
*
|
|
661
|
+
* @param {TemporalDate} from - The starting date.
|
|
662
|
+
* @param {TemporalDate} to - The ending date.
|
|
663
|
+
* @returns {number} The difference in years between the two dates.
|
|
664
|
+
* @example
|
|
665
|
+
* const dateString1 = "2023-05-10"
|
|
666
|
+
* const dateString2 = "2023-05-12"
|
|
667
|
+
* const fromDate = new Date(dateString1);
|
|
668
|
+
* const toDate = new Date(dateString2);
|
|
669
|
+
* const result = differenceInYearsUtil(fromDate, toDate);
|
|
670
|
+
* Output: 0 (if fromDate and toDate are in the same year)
|
|
671
|
+
* @example
|
|
672
|
+
* const dateString1 = "2023-05-10"
|
|
673
|
+
* const dateString2 = "2025-05-18"
|
|
674
|
+
* const fromDate = new Date(dateString1);
|
|
675
|
+
* const toDate = new Date(dateString2);
|
|
676
|
+
* const result = differenceInWeeksUtil(fromDate, toDate);
|
|
677
|
+
* Output: 2
|
|
678
|
+
*/
|
|
679
|
+
const differenceInYearsUtil = (from, to) => {
|
|
680
|
+
let until = new Temporal.Duration();
|
|
681
|
+
try {
|
|
682
|
+
until = toZonedDateTimeUtil(from).toPlainDate().until(toZonedDateTimeUtil(to).toPlainDate(), {
|
|
683
|
+
largestUnit: "year",
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
catch (error) {
|
|
687
|
+
// eslint-disable-next-line no-console
|
|
688
|
+
console.warn("Error: ", error);
|
|
689
|
+
}
|
|
690
|
+
return until.years;
|
|
691
|
+
};
|
|
621
692
|
/**
|
|
622
693
|
* Calculates the difference in months between two dates.
|
|
623
694
|
*
|
|
@@ -1062,7 +1133,7 @@ const getDurationFormat = (duration, locale, format) => {
|
|
|
1062
1133
|
* convert a number to a type
|
|
1063
1134
|
*
|
|
1064
1135
|
* @param {number} number - 230.45
|
|
1065
|
-
* @param {TemporalTimeTypes} unit - "days | "hours" | minutes
|
|
1136
|
+
* @param {TemporalTimeTypes} unit - "days | "hours" | "minutes" | "seconds"
|
|
1066
1137
|
* @param {Temporal.TimeUnit} roundAt - smallest unit to show
|
|
1067
1138
|
*/
|
|
1068
1139
|
const toDuration = (number, unit, roundAt) => {
|
|
@@ -1077,6 +1148,8 @@ const toDuration = (number, unit, roundAt) => {
|
|
|
1077
1148
|
return Temporal.Duration.from(durationBuilder(number, "minutes")).round({
|
|
1078
1149
|
smallestUnit: roundAt !== null && roundAt !== void 0 ? roundAt : "seconds",
|
|
1079
1150
|
});
|
|
1151
|
+
case "seconds":
|
|
1152
|
+
return Temporal.Duration.from(durationBuilder(number, "seconds")).round({ smallestUnit: roundAt !== null && roundAt !== void 0 ? roundAt : "seconds" });
|
|
1080
1153
|
default:
|
|
1081
1154
|
throw new Error("Not a valid unit", { cause: unit });
|
|
1082
1155
|
}
|
|
@@ -1086,7 +1159,7 @@ const defaultDuration = { days: 0, hours: 0, minutes: 0, seconds: 0 };
|
|
|
1086
1159
|
* Build a duration string
|
|
1087
1160
|
*
|
|
1088
1161
|
* @param number - number to convert
|
|
1089
|
-
* @param unit - "days | "hours" | minutes
|
|
1162
|
+
* @param unit - "days | "hours" | "minutes" | "seconds"
|
|
1090
1163
|
*/
|
|
1091
1164
|
const durationBuilder = (number, unit) => {
|
|
1092
1165
|
const isSigned = Math.sign(number) === -1;
|
|
@@ -1110,6 +1183,11 @@ const durationBuilder = (number, unit) => {
|
|
|
1110
1183
|
return `${sign}PT${unsignedNumber}M`;
|
|
1111
1184
|
}
|
|
1112
1185
|
return `${sign}PT${minutes}M${seconds}S`;
|
|
1186
|
+
case "seconds":
|
|
1187
|
+
if (isInt) {
|
|
1188
|
+
return `${sign}PT${unsignedNumber}S`;
|
|
1189
|
+
}
|
|
1190
|
+
return `${sign}PT${seconds}S`;
|
|
1113
1191
|
default:
|
|
1114
1192
|
return `PT0S`;
|
|
1115
1193
|
}
|
|
@@ -1118,7 +1196,7 @@ const durationBuilder = (number, unit) => {
|
|
|
1118
1196
|
* Calculate time in days, hours, minutes, and seconds
|
|
1119
1197
|
*
|
|
1120
1198
|
* @param {number} number - number to convert
|
|
1121
|
-
* @param {TemporalTimeTypes} unit - "days | "hours" | minutes
|
|
1199
|
+
* @param {TemporalTimeTypes} unit - "days | "hours" | "minutes" | "seconds"
|
|
1122
1200
|
*/
|
|
1123
1201
|
const calculateTime = (number, unit) => {
|
|
1124
1202
|
switch (unit) {
|
|
@@ -1363,5 +1441,142 @@ const daysUtil = (format, locale) => {
|
|
|
1363
1441
|
}
|
|
1364
1442
|
return days;
|
|
1365
1443
|
};
|
|
1444
|
+
/**
|
|
1445
|
+
* Returns the UTC date-time for the given time zone.
|
|
1446
|
+
*
|
|
1447
|
+
* @param {Date} from - The date to convert to UTC.
|
|
1448
|
+
* @param {string} timeZoneId - The time zone of the input date.
|
|
1449
|
+
*/
|
|
1450
|
+
const getUTCFromTimeZonedUtil = (from, timeZoneId) => {
|
|
1451
|
+
const zonedDateTime = toZonedDateTimeUtil(from, timeZoneId);
|
|
1452
|
+
return toDateUtil(zonedDateTime.toInstant().toZonedDateTimeISO("UTC"));
|
|
1453
|
+
};
|
|
1454
|
+
/**
|
|
1455
|
+
* @name getWeekUtil
|
|
1456
|
+
* @summary Get the local week index of the given date.
|
|
1457
|
+
* @description
|
|
1458
|
+
* Get the local week index of the given date using Temporal.
|
|
1459
|
+
* The exact calculation depends on the values of `weekStartsOn` (the index of the first day of the week)
|
|
1460
|
+
* and `firstWeekContainsDate` (the day in January, which is always in the first week of the week-numbering year).
|
|
1461
|
+
* @param {Temporal.PlainDate | Date | string} date - The given date (can be either Temporal.PlainDate, a native Date, or an ISO date string).
|
|
1462
|
+
* @param {GetWeekUtilOptions} [options] - Optional settings for the week calculation.
|
|
1463
|
+
* @returns {number} - The week number of the year.
|
|
1464
|
+
* @example
|
|
1465
|
+
* // Which week of the local week numbering year is 2 January 2005 with default options?
|
|
1466
|
+
* const result = getWeekUtil(new Date(2005, 0, 2))
|
|
1467
|
+
* //=> 2
|
|
1468
|
+
* @example
|
|
1469
|
+
* // Which week of the local week numbering year is 2 January 2005, if Monday is the first day of the week,
|
|
1470
|
+
* // and the first week of the year always contains 4 January?
|
|
1471
|
+
* const result = getWeekUtil("2005-01-02T00:00:00.000Z", {
|
|
1472
|
+
* weekStartsOn: 1,
|
|
1473
|
+
* firstWeekContainsDate: 4
|
|
1474
|
+
* })
|
|
1475
|
+
* //=> 53
|
|
1476
|
+
*/
|
|
1477
|
+
const getWeekUtil = (date, options) => {
|
|
1478
|
+
const _date = convertToPlainDate(date);
|
|
1479
|
+
const { weekStartsOn = 0, firstWeekContainsDate = 1 } = options || {};
|
|
1480
|
+
const startOfCurrentWeek = startOfWeek(_date, weekStartsOn);
|
|
1481
|
+
const startOfWeekYear = startOfWeek(Temporal.PlainDate.from({
|
|
1482
|
+
year: _date.year,
|
|
1483
|
+
month: 1,
|
|
1484
|
+
day: firstWeekContainsDate,
|
|
1485
|
+
}), weekStartsOn);
|
|
1486
|
+
const diffInDays = startOfCurrentWeek.since(startOfWeekYear).total({ unit: "days" });
|
|
1487
|
+
return Math.floor(diffInDays / 7) + 1;
|
|
1488
|
+
};
|
|
1489
|
+
/**
|
|
1490
|
+
* @name convertMillisecondsUtil
|
|
1491
|
+
* @summary Convert milliseconds using Temporal.
|
|
1492
|
+
* @description
|
|
1493
|
+
* Convert milliseconds to the specified unit using Temporal.
|
|
1494
|
+
* @param {number} milliseconds - The number of milliseconds to convert.
|
|
1495
|
+
* @returns {number} - The converted value.
|
|
1496
|
+
* @example
|
|
1497
|
+
* const result = convertMillisecondsUtil(60000, "minute")
|
|
1498
|
+
* //=> 1
|
|
1499
|
+
* @example
|
|
1500
|
+
* const result = convertMillisecondsUtil(120000, "second")
|
|
1501
|
+
* //=> 120
|
|
1502
|
+
* @example
|
|
1503
|
+
* const result = convertMillisecondsUtil(3600000, "hour")
|
|
1504
|
+
* //=> 1
|
|
1505
|
+
*/
|
|
1506
|
+
const convertMillisecondsUtil = (milliseconds, unit) => {
|
|
1507
|
+
return Temporal.Duration.from({ milliseconds }).total({ unit });
|
|
1508
|
+
};
|
|
1509
|
+
/**
|
|
1510
|
+
* @name convertSecondsUtil
|
|
1511
|
+
* @summary Convert seconds using Temporal.
|
|
1512
|
+
* @description
|
|
1513
|
+
* Convert seconds to the specified unit using Temporal.
|
|
1514
|
+
* @param {number} seconds - The number of seconds to convert.
|
|
1515
|
+
* @returns {number} - The converted value.
|
|
1516
|
+
* @example
|
|
1517
|
+
* const result = convertSecondsUtil(60, "minute")
|
|
1518
|
+
* //=> 1
|
|
1519
|
+
* @example
|
|
1520
|
+
* const result = convertSecondsUtil(120, "second")
|
|
1521
|
+
* //=> 120
|
|
1522
|
+
*/
|
|
1523
|
+
const convertSecondsUtil = (seconds, unit) => {
|
|
1524
|
+
return Temporal.Duration.from({ seconds }).total({ unit });
|
|
1525
|
+
};
|
|
1526
|
+
/**
|
|
1527
|
+
* @name convertMinutesUtil
|
|
1528
|
+
* @summary Convert seconds using Temporal.
|
|
1529
|
+
* @description
|
|
1530
|
+
* Convert minutes to the specified unit using Temporal.
|
|
1531
|
+
* @param {number} minutes - The number of minutes to convert.
|
|
1532
|
+
* @returns {number} - The converted value.
|
|
1533
|
+
* @example
|
|
1534
|
+
* const result = convertMinutesUtil(60, "minute")
|
|
1535
|
+
* //=> 1
|
|
1536
|
+
* @example
|
|
1537
|
+
* const result = convertMinutesUtil(120, "second")
|
|
1538
|
+
* //=> 120
|
|
1539
|
+
*/
|
|
1540
|
+
const convertMinutesUtil = (minutes, unit) => {
|
|
1541
|
+
return Temporal.Duration.from({ minutes }).total({ unit });
|
|
1542
|
+
};
|
|
1543
|
+
/**
|
|
1544
|
+
* @name convertHoursUtil
|
|
1545
|
+
* @summary Convert hours using Temporal.
|
|
1546
|
+
* @description
|
|
1547
|
+
* Convert hours to the specified unit using Temporal.
|
|
1548
|
+
* @param {number} hours - The number of hours to convert.
|
|
1549
|
+
* @returns {number} - The converted value.
|
|
1550
|
+
* @example
|
|
1551
|
+
* const result = convertHoursUtil(24, "day")
|
|
1552
|
+
* //=> 1
|
|
1553
|
+
* @example
|
|
1554
|
+
* const result = convertHoursUtil(48, "hour")
|
|
1555
|
+
* //=> 48
|
|
1556
|
+
*/
|
|
1557
|
+
const convertHoursUtil = (hours, unit) => {
|
|
1558
|
+
return Temporal.Duration.from({ hours }).total({ unit });
|
|
1559
|
+
};
|
|
1560
|
+
const convertToPlainDate = (date) => {
|
|
1561
|
+
if (date instanceof Temporal.PlainDate) {
|
|
1562
|
+
return date;
|
|
1563
|
+
}
|
|
1564
|
+
if (typeof date === "string") {
|
|
1565
|
+
return Temporal.PlainDate.from(date);
|
|
1566
|
+
}
|
|
1567
|
+
if (date instanceof Date) {
|
|
1568
|
+
const dateString = date.toISOString().split("T")[0];
|
|
1569
|
+
if (!dateString) {
|
|
1570
|
+
throw new Error("Invalid Date object. Cannot convert to Temporal.PlainDate.");
|
|
1571
|
+
}
|
|
1572
|
+
return Temporal.PlainDate.from(dateString);
|
|
1573
|
+
}
|
|
1574
|
+
throw new Error("Unsupported date format. Please provide a Temporal.PlainDate, Date, or ISO string.");
|
|
1575
|
+
};
|
|
1576
|
+
const startOfWeek = (date, weekStartsOn) => {
|
|
1577
|
+
const dayOfWeek = date.dayOfWeek;
|
|
1578
|
+
const diff = (dayOfWeek - weekStartsOn + 7) % 7;
|
|
1579
|
+
return date.subtract({ days: diff });
|
|
1580
|
+
};
|
|
1366
1581
|
|
|
1367
|
-
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 };
|
|
1582
|
+
export { addDaysUtil, addHoursUtil, addMinutesUtil, addMonthsUtil, addWeeksUtil, addYearsUtil, convertHoursUtil, convertMillisecondsUtil, convertMinutesUtil, convertSecondsUtil, dayNameUtil, daysUtil, differenceInDaysUtil, differenceInHoursUtil, differenceInMinutesUtil, differenceInMonthsUtil, differenceInSecondsUtil, differenceInWeeksUtil, differenceInYearsUtil, endOfDayUtil, endOfHourUtil, endOfMinuteUtil, endOfMonthUtil, endOfWeekUtil, formatDateUtil, formatRangeUtil, getDurationFormat, getTimeZone, getTimeZoneOffset, getUTCFromTimeZonedUtil, getWeekUtil, 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,15 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trackunit/date-and-time-utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.49",
|
|
4
4
|
"repository": "https://github.com/Trackunit/manager",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
6
|
"engines": {
|
|
7
7
|
"node": ">=20.x"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"@formatjs/intl-enumerator": "^1.
|
|
10
|
+
"@formatjs/intl-enumerator": "^1.8.1",
|
|
11
11
|
"@js-temporal/polyfill": "^0.4.4",
|
|
12
|
-
"@formatjs/intl-durationformat": "^0.
|
|
12
|
+
"@formatjs/intl-durationformat": "^0.6.1"
|
|
13
13
|
},
|
|
14
14
|
"module": "./index.esm.js",
|
|
15
15
|
"main": "./index.cjs.js",
|
|
@@ -2,8 +2,9 @@ import "@formatjs/intl-durationformat/polyfill";
|
|
|
2
2
|
import { Temporal } from "@js-temporal/polyfill";
|
|
3
3
|
export type TemporalDate = Temporal.Instant | Temporal.ZonedDateTime | Date;
|
|
4
4
|
type TemporalFormatTypes = Intl.DateTimeFormatOptions;
|
|
5
|
-
type TemporalTimeTypes = Extract<Intl.RelativeTimeFormatUnit, "days" | "hours" | "minutes">;
|
|
5
|
+
type TemporalTimeTypes = Extract<Intl.RelativeTimeFormatUnit, "days" | "hours" | "minutes" | "seconds">;
|
|
6
6
|
type TemporalFormatSelector = "dateOnly" | "timeOnly";
|
|
7
|
+
type HourCycle = "h12" | "h23";
|
|
7
8
|
export type TemporalFormatStyle = Intl.ListFormatStyle;
|
|
8
9
|
export interface DateRange {
|
|
9
10
|
start: Date;
|
|
@@ -78,7 +79,7 @@ export declare const timeZonesAvailable: string[];
|
|
|
78
79
|
* const formattedDate = formatDateUtil(date, format, timeZone, locale);
|
|
79
80
|
* Output: "5/10/23, 12:00:00 AM EDT"
|
|
80
81
|
*/
|
|
81
|
-
export declare const formatDateUtil: (date: TemporalDate, format?: TemporalFormat, timeZone?: string, locale?: string) => string;
|
|
82
|
+
export declare const formatDateUtil: (date: TemporalDate, format?: TemporalFormat, timeZone?: string, locale?: string, hourCycle?: HourCycle) => string;
|
|
82
83
|
/**
|
|
83
84
|
* Formats a date range.
|
|
84
85
|
*
|
|
@@ -369,7 +370,7 @@ export declare const startOfWeekUtil: (date: TemporalDate, timeZone?: string) =>
|
|
|
369
370
|
* const dateString = "2023-05-10T10:30:00.000Z"
|
|
370
371
|
* const date = new Date(dateString);
|
|
371
372
|
* const result = startOfWeekUtil(date);
|
|
372
|
-
* Output: Temporal.ZonedDateTime { year: 2023, month: 5, day: 14, hour:
|
|
373
|
+
* Output: Temporal.ZonedDateTime { year: 2023, month: 5, day: 14, hour: 23, minute: 59, second: 59, millisecond: 999, microsecond: 0, nanosecond: 0, timeZone: Temporal.TimeZone { id: "America/New_York" }, calendar: Temporal.Calendar { id: "iso8601" } }
|
|
373
374
|
*/
|
|
374
375
|
export declare const endOfWeekUtil: (date: TemporalDate, timeZone?: string) => Temporal.ZonedDateTime;
|
|
375
376
|
/**
|
|
@@ -395,9 +396,30 @@ export declare const startOfMonthUtil: (date: TemporalDate, timeZone?: string) =
|
|
|
395
396
|
* const dateString = "2023-05-10"
|
|
396
397
|
* const date = new Date(dateString);
|
|
397
398
|
* const result = endOfMonthUtil(date);
|
|
398
|
-
* Output: Temporal.ZonedDateTime { year: 2023, month: 5, day: 31, hour:
|
|
399
|
+
* Output: Temporal.ZonedDateTime { year: 2023, month: 5, day: 31, hour: 23, minute: 59, second: 59, millisecond: 999, microsecond: 0, nanosecond: 0, timeZone: Temporal.TimeZone { id: "America/New_York" }, calendar: Temporal.Calendar { id: "iso8601" } }
|
|
399
400
|
*/
|
|
400
401
|
export declare const endOfMonthUtil: (date: TemporalDate, timeZone?: string) => Temporal.ZonedDateTime;
|
|
402
|
+
/**
|
|
403
|
+
* Calculates the difference in seconds between two dates.
|
|
404
|
+
*
|
|
405
|
+
* @param {TemporalDate} from - The starting date.
|
|
406
|
+
* @param {TemporalDate} to - The ending date.
|
|
407
|
+
* @returns {number} The difference in seconds between the two dates.
|
|
408
|
+
* @example
|
|
409
|
+
* const dateString = "2023-05-10T10:30:00.000Z"
|
|
410
|
+
* const fromDate = new Date(dateString);
|
|
411
|
+
* const toDate = new Date(dateString);
|
|
412
|
+
* const result = differenceInSecondsUtil(fromDate, toDate);
|
|
413
|
+
* Output: 0 (if fromDate and toDate are the same)
|
|
414
|
+
* @example
|
|
415
|
+
* const dateString1 = "2023-05-10T10:30:00.000Z"
|
|
416
|
+
* const dateString2 = "2023-05-10T10:35:00.000Z"
|
|
417
|
+
* const fromDate = new Date(dateString1);
|
|
418
|
+
* const toDate = new Date(dateString2);
|
|
419
|
+
* const result = differenceInSecondsUtil(fromDate, toDate);
|
|
420
|
+
* Output: 5
|
|
421
|
+
*/
|
|
422
|
+
export declare const differenceInSecondsUtil: (from: TemporalDate, to: TemporalDate) => number;
|
|
401
423
|
/**
|
|
402
424
|
* Calculates the difference in minutes between two dates.
|
|
403
425
|
*
|
|
@@ -485,6 +507,28 @@ export declare const differenceInDaysUtil: (from: TemporalDate, to: TemporalDate
|
|
|
485
507
|
* Output: 1
|
|
486
508
|
*/
|
|
487
509
|
export declare const differenceInWeeksUtil: (from: TemporalDate, to: TemporalDate) => number;
|
|
510
|
+
/**
|
|
511
|
+
* Calculates the difference in years between two dates.
|
|
512
|
+
*
|
|
513
|
+
* @param {TemporalDate} from - The starting date.
|
|
514
|
+
* @param {TemporalDate} to - The ending date.
|
|
515
|
+
* @returns {number} The difference in years between the two dates.
|
|
516
|
+
* @example
|
|
517
|
+
* const dateString1 = "2023-05-10"
|
|
518
|
+
* const dateString2 = "2023-05-12"
|
|
519
|
+
* const fromDate = new Date(dateString1);
|
|
520
|
+
* const toDate = new Date(dateString2);
|
|
521
|
+
* const result = differenceInYearsUtil(fromDate, toDate);
|
|
522
|
+
* Output: 0 (if fromDate and toDate are in the same year)
|
|
523
|
+
* @example
|
|
524
|
+
* const dateString1 = "2023-05-10"
|
|
525
|
+
* const dateString2 = "2025-05-18"
|
|
526
|
+
* const fromDate = new Date(dateString1);
|
|
527
|
+
* const toDate = new Date(dateString2);
|
|
528
|
+
* const result = differenceInWeeksUtil(fromDate, toDate);
|
|
529
|
+
* Output: 2
|
|
530
|
+
*/
|
|
531
|
+
export declare const differenceInYearsUtil: (from: TemporalDate, to: TemporalDate) => number;
|
|
488
532
|
/**
|
|
489
533
|
* Calculates the difference in months between two dates.
|
|
490
534
|
*
|
|
@@ -754,7 +798,7 @@ export declare const getDurationFormat: (duration: Intl.Duration, locale?: strin
|
|
|
754
798
|
* convert a number to a type
|
|
755
799
|
*
|
|
756
800
|
* @param {number} number - 230.45
|
|
757
|
-
* @param {TemporalTimeTypes} unit - "days | "hours" | minutes
|
|
801
|
+
* @param {TemporalTimeTypes} unit - "days | "hours" | "minutes" | "seconds"
|
|
758
802
|
* @param {Temporal.TimeUnit} roundAt - smallest unit to show
|
|
759
803
|
*/
|
|
760
804
|
export declare const toDuration: (number: number, unit: TemporalTimeTypes, roundAt?: Extract<Temporal.TimeUnit, "hour" | "minute" | "second">) => Intl.Duration;
|
|
@@ -873,4 +917,103 @@ export declare const dayNameUtil: (date: TemporalDate, format: TemporalFormatSty
|
|
|
873
917
|
* @param {string} locale - The locale to use for formatting. If not provided, the system's default locale will be used.
|
|
874
918
|
*/
|
|
875
919
|
export declare const daysUtil: (format?: TemporalFormatStyle, locale?: string) => string[];
|
|
920
|
+
/**
|
|
921
|
+
* Returns the UTC date-time for the given time zone.
|
|
922
|
+
*
|
|
923
|
+
* @param {Date} from - The date to convert to UTC.
|
|
924
|
+
* @param {string} timeZoneId - The time zone of the input date.
|
|
925
|
+
*/
|
|
926
|
+
export declare const getUTCFromTimeZonedUtil: (from: Date, timeZoneId: string) => Date;
|
|
927
|
+
interface GetWeekUtilOptions {
|
|
928
|
+
weekStartsOn?: number;
|
|
929
|
+
firstWeekContainsDate?: number;
|
|
930
|
+
locale?: string;
|
|
931
|
+
}
|
|
932
|
+
/**
|
|
933
|
+
* @name getWeekUtil
|
|
934
|
+
* @summary Get the local week index of the given date.
|
|
935
|
+
* @description
|
|
936
|
+
* Get the local week index of the given date using Temporal.
|
|
937
|
+
* The exact calculation depends on the values of `weekStartsOn` (the index of the first day of the week)
|
|
938
|
+
* and `firstWeekContainsDate` (the day in January, which is always in the first week of the week-numbering year).
|
|
939
|
+
* @param {Temporal.PlainDate | Date | string} date - The given date (can be either Temporal.PlainDate, a native Date, or an ISO date string).
|
|
940
|
+
* @param {GetWeekUtilOptions} [options] - Optional settings for the week calculation.
|
|
941
|
+
* @returns {number} - The week number of the year.
|
|
942
|
+
* @example
|
|
943
|
+
* // Which week of the local week numbering year is 2 January 2005 with default options?
|
|
944
|
+
* const result = getWeekUtil(new Date(2005, 0, 2))
|
|
945
|
+
* //=> 2
|
|
946
|
+
* @example
|
|
947
|
+
* // Which week of the local week numbering year is 2 January 2005, if Monday is the first day of the week,
|
|
948
|
+
* // and the first week of the year always contains 4 January?
|
|
949
|
+
* const result = getWeekUtil("2005-01-02T00:00:00.000Z", {
|
|
950
|
+
* weekStartsOn: 1,
|
|
951
|
+
* firstWeekContainsDate: 4
|
|
952
|
+
* })
|
|
953
|
+
* //=> 53
|
|
954
|
+
*/
|
|
955
|
+
export declare const getWeekUtil: (date: Temporal.PlainDate | Date | string, options?: GetWeekUtilOptions) => number;
|
|
956
|
+
/**
|
|
957
|
+
* @name convertMillisecondsUtil
|
|
958
|
+
* @summary Convert milliseconds using Temporal.
|
|
959
|
+
* @description
|
|
960
|
+
* Convert milliseconds to the specified unit using Temporal.
|
|
961
|
+
* @param {number} milliseconds - The number of milliseconds to convert.
|
|
962
|
+
* @returns {number} - The converted value.
|
|
963
|
+
* @example
|
|
964
|
+
* const result = convertMillisecondsUtil(60000, "minute")
|
|
965
|
+
* //=> 1
|
|
966
|
+
* @example
|
|
967
|
+
* const result = convertMillisecondsUtil(120000, "second")
|
|
968
|
+
* //=> 120
|
|
969
|
+
* @example
|
|
970
|
+
* const result = convertMillisecondsUtil(3600000, "hour")
|
|
971
|
+
* //=> 1
|
|
972
|
+
*/
|
|
973
|
+
export declare const convertMillisecondsUtil: (milliseconds: number, unit: Extract<Temporal.TimeUnit, "hour" | "minute" | "second" | "millisecond">) => number;
|
|
974
|
+
/**
|
|
975
|
+
* @name convertSecondsUtil
|
|
976
|
+
* @summary Convert seconds using Temporal.
|
|
977
|
+
* @description
|
|
978
|
+
* Convert seconds to the specified unit using Temporal.
|
|
979
|
+
* @param {number} seconds - The number of seconds to convert.
|
|
980
|
+
* @returns {number} - The converted value.
|
|
981
|
+
* @example
|
|
982
|
+
* const result = convertSecondsUtil(60, "minute")
|
|
983
|
+
* //=> 1
|
|
984
|
+
* @example
|
|
985
|
+
* const result = convertSecondsUtil(120, "second")
|
|
986
|
+
* //=> 120
|
|
987
|
+
*/
|
|
988
|
+
export declare const convertSecondsUtil: (seconds: number, unit: Extract<Temporal.TimeUnit, "hour" | "minute" | "second" | "millisecond">) => number;
|
|
989
|
+
/**
|
|
990
|
+
* @name convertMinutesUtil
|
|
991
|
+
* @summary Convert seconds using Temporal.
|
|
992
|
+
* @description
|
|
993
|
+
* Convert minutes to the specified unit using Temporal.
|
|
994
|
+
* @param {number} minutes - The number of minutes to convert.
|
|
995
|
+
* @returns {number} - The converted value.
|
|
996
|
+
* @example
|
|
997
|
+
* const result = convertMinutesUtil(60, "minute")
|
|
998
|
+
* //=> 1
|
|
999
|
+
* @example
|
|
1000
|
+
* const result = convertMinutesUtil(120, "second")
|
|
1001
|
+
* //=> 120
|
|
1002
|
+
*/
|
|
1003
|
+
export declare const convertMinutesUtil: (minutes: number, unit: Extract<Temporal.TimeUnit, "hour" | "minute" | "second" | "millisecond">) => number;
|
|
1004
|
+
/**
|
|
1005
|
+
* @name convertHoursUtil
|
|
1006
|
+
* @summary Convert hours using Temporal.
|
|
1007
|
+
* @description
|
|
1008
|
+
* Convert hours to the specified unit using Temporal.
|
|
1009
|
+
* @param {number} hours - The number of hours to convert.
|
|
1010
|
+
* @returns {number} - The converted value.
|
|
1011
|
+
* @example
|
|
1012
|
+
* const result = convertHoursUtil(24, "day")
|
|
1013
|
+
* //=> 1
|
|
1014
|
+
* @example
|
|
1015
|
+
* const result = convertHoursUtil(48, "hour")
|
|
1016
|
+
* //=> 48
|
|
1017
|
+
*/
|
|
1018
|
+
export declare const convertHoursUtil: (hours: number, unit: Extract<Temporal.TimeUnit, "hour" | "minute" | "second" | "millisecond">) => number;
|
|
876
1019
|
export {};
|