@trackunit/date-and-time-utils 1.20.52 → 1.20.54

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 CHANGED
@@ -573,6 +573,21 @@ const endOfMonthUtil = (date, timeZone) => {
573
573
  const totalDays = day.daysInMonth;
574
574
  return addDaysUtil(day, totalDays - currentDay);
575
575
  };
576
+ /**
577
+ * One implementation of the full zoned date-time diff, so seconds, minutes, and hours
578
+ * cannot drift apart. `largestUnit` keeps the whole span in that field.
579
+ */
580
+ const differenceInZonedUnit = (from, to, largestUnit) => {
581
+ let until = new polyfill.Temporal.Duration();
582
+ try {
583
+ until = toZonedDateTimeUtil(from).until(toZonedDateTimeUtil(to), { largestUnit });
584
+ }
585
+ catch (error) {
586
+ // eslint-disable-next-line no-console
587
+ console.warn("Error: ", error);
588
+ }
589
+ return until[largestUnit];
590
+ };
576
591
  /**
577
592
  * Calculates the difference in seconds between two dates.
578
593
  *
@@ -591,20 +606,10 @@ const endOfMonthUtil = (date, timeZone) => {
591
606
  * const fromDate = new Date(dateString1);
592
607
  * const toDate = new Date(dateString2);
593
608
  * const result = differenceInSecondsUtil(fromDate, toDate);
594
- * Output: 5
609
+ * Output: 300
595
610
  */
596
611
  const differenceInSecondsUtil = (from, to) => {
597
- let until = new polyfill.Temporal.Duration();
598
- try {
599
- until = toZonedDateTimeUtil(from).toPlainTime().until(toZonedDateTimeUtil(to).toPlainTime(), {
600
- largestUnit: "seconds",
601
- });
602
- }
603
- catch (error) {
604
- // eslint-disable-next-line no-console
605
- console.warn("Error: ", error);
606
- }
607
- return until.seconds;
612
+ return differenceInZonedUnit(from, to, "seconds");
608
613
  };
609
614
  /**
610
615
  * Calculates the difference in minutes between two dates.
@@ -627,17 +632,7 @@ const differenceInSecondsUtil = (from, to) => {
627
632
  * Output: 5
628
633
  */
629
634
  const differenceInMinutesUtil = (from, to) => {
630
- let until = new polyfill.Temporal.Duration();
631
- try {
632
- until = toZonedDateTimeUtil(from).toPlainTime().until(toZonedDateTimeUtil(to).toPlainTime(), {
633
- largestUnit: "minutes",
634
- });
635
- }
636
- catch (error) {
637
- // eslint-disable-next-line no-console
638
- console.warn("Error: ", error);
639
- }
640
- return until.minutes;
635
+ return differenceInZonedUnit(from, to, "minutes");
641
636
  };
642
637
  /**
643
638
  * Calculates the difference in hours between two dates.
@@ -661,17 +656,7 @@ const differenceInMinutesUtil = (from, to) => {
661
656
  * Output: 2
662
657
  */
663
658
  const differenceInHoursUtil = (from, to) => {
664
- let until = new polyfill.Temporal.Duration();
665
- try {
666
- until = toZonedDateTimeUtil(from).toPlainTime().until(toZonedDateTimeUtil(to).toPlainTime(), {
667
- largestUnit: "hours",
668
- });
669
- }
670
- catch (error) {
671
- // eslint-disable-next-line no-console
672
- console.warn("Error: ", error);
673
- }
674
- return until.hours;
659
+ return differenceInZonedUnit(from, to, "hours");
675
660
  };
676
661
  /**
677
662
  * Calculates the difference in days between two dates.
@@ -897,7 +882,7 @@ const isSameDayUtil = (date, compareTo) => {
897
882
  const isSameWeekUtil = (date) => {
898
883
  const today = toZonedDateTimeUtil(polyfill.Temporal.Now.instant());
899
884
  const input = toZonedDateTimeUtil(date);
900
- return today.weekOfYear === input.weekOfYear && today.year === input.year;
885
+ return today.weekOfYear === input.weekOfYear && today.yearOfWeek === input.yearOfWeek;
901
886
  };
902
887
  /**
903
888
  * Checks if a date is same month as current.
@@ -1227,11 +1212,6 @@ const timeSinceInDays = (from, to, timeZone, locale, style) => {
1227
1212
  * @param {string} [timeZone] - The time zone to use for the calculations. Defaults to the local time zone.
1228
1213
  * @param {string} [locale] - The locale to use for formatting the duration. Defaults to the user's locale.
1229
1214
  * @returns {string} - The formatted duration in months.
1230
- * @remarks When both `from` and `to` fall in a leap year, the function falls back to
1231
- * `timeSinceInDays` and returns the raw day count for the whole span. Callers relying on a
1232
- * specific unit — especially with `style="narrow-compact"`, where the unit is visible —
1233
- * should be aware that the output can be an arbitrarily large day count (e.g. `"60d"` for a
1234
- * two-month gap) instead of months on those inputs.
1235
1215
  * @example
1236
1216
  * const startDate = "2023-05-10"
1237
1217
  * const endDate = "2023-07-11"
@@ -1240,10 +1220,6 @@ const timeSinceInDays = (from, to, timeZone, locale, style) => {
1240
1220
  * Output: "2 months ago"
1241
1221
  */
1242
1222
  const timeSinceInMonths = (from, to, timeZone, locale, style) => {
1243
- const isLeapYear = toZonedDateTimeUtil(from, timeZone).inLeapYear && toZonedDateTimeUtil(to, timeZone).inLeapYear;
1244
- if (isLeapYear) {
1245
- return timeSinceInDays(from, to, timeZone, locale, style);
1246
- }
1247
1223
  let since = new polyfill.Temporal.Duration();
1248
1224
  try {
1249
1225
  since = toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
@@ -1265,11 +1241,6 @@ const timeSinceInMonths = (from, to, timeZone, locale, style) => {
1265
1241
  * @param {string} [timeZone] - The time zone to use for the calculations. Defaults to the local time zone.
1266
1242
  * @param {string} [locale] - The locale to use for formatting the duration. Defaults to the user's locale.
1267
1243
  * @returns {string} - The formatted duration in years.
1268
- * @remarks When both `from` and `to` fall in a leap year, the function falls back to
1269
- * `timeSinceInDays` and returns the raw day count for the whole span. Callers relying on a
1270
- * specific unit — especially with `style="narrow-compact"`, where the unit is visible —
1271
- * should be aware that the output can be an arbitrarily large day count (e.g. `"1462d"` for
1272
- * a ~4-year gap between two leap-year endpoints) instead of years on those inputs.
1273
1244
  * @example
1274
1245
  * const startDate = "2023-05-10"
1275
1246
  * const endDate = "2024-05-11"
@@ -1278,10 +1249,6 @@ const timeSinceInMonths = (from, to, timeZone, locale, style) => {
1278
1249
  * Output: "1 year ago"
1279
1250
  */
1280
1251
  const timeSinceInYears = (from, to, timeZone, locale, style) => {
1281
- const isLeapYear = toZonedDateTimeUtil(from, timeZone).inLeapYear && toZonedDateTimeUtil(to, timeZone).inLeapYear;
1282
- if (isLeapYear) {
1283
- return timeSinceInDays(from, to, timeZone, locale, style);
1284
- }
1285
1252
  let since = new polyfill.Temporal.Duration();
1286
1253
  try {
1287
1254
  since = toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
@@ -1659,11 +1626,15 @@ const getWeekUtil = (date, options) => {
1659
1626
  const _date = convertToPlainDate(date);
1660
1627
  const { weekStartsOn = 0, firstWeekContainsDate = 1 } = options || {};
1661
1628
  const startOfCurrentWeek = startOfWeek(_date, weekStartsOn);
1662
- const startOfWeekYear = startOfWeek(polyfill.Temporal.PlainDate.from({
1663
- year: _date.year,
1664
- month: 1,
1665
- day: firstWeekContainsDate,
1666
- }), weekStartsOn);
1629
+ const getStartOfWeekYear = (year) => startOfWeek(polyfill.Temporal.PlainDate.from({ year, month: 1, day: firstWeekContainsDate }), weekStartsOn);
1630
+ const startOfNextWeekYear = getStartOfWeekYear(_date.year + 1);
1631
+ if (polyfill.Temporal.PlainDate.compare(_date, startOfNextWeekYear) >= 0) {
1632
+ return 1;
1633
+ }
1634
+ const startOfThisWeekYear = getStartOfWeekYear(_date.year);
1635
+ const startOfWeekYear = polyfill.Temporal.PlainDate.compare(_date, startOfThisWeekYear) < 0
1636
+ ? getStartOfWeekYear(_date.year - 1)
1637
+ : startOfThisWeekYear;
1667
1638
  const diffInDays = startOfCurrentWeek.since(startOfWeekYear).total({ unit: "days" });
1668
1639
  return Math.floor(diffInDays / 7) + 1;
1669
1640
  };
package/index.esm.js CHANGED
@@ -572,6 +572,21 @@ const endOfMonthUtil = (date, timeZone) => {
572
572
  const totalDays = day.daysInMonth;
573
573
  return addDaysUtil(day, totalDays - currentDay);
574
574
  };
575
+ /**
576
+ * One implementation of the full zoned date-time diff, so seconds, minutes, and hours
577
+ * cannot drift apart. `largestUnit` keeps the whole span in that field.
578
+ */
579
+ const differenceInZonedUnit = (from, to, largestUnit) => {
580
+ let until = new Temporal.Duration();
581
+ try {
582
+ until = toZonedDateTimeUtil(from).until(toZonedDateTimeUtil(to), { largestUnit });
583
+ }
584
+ catch (error) {
585
+ // eslint-disable-next-line no-console
586
+ console.warn("Error: ", error);
587
+ }
588
+ return until[largestUnit];
589
+ };
575
590
  /**
576
591
  * Calculates the difference in seconds between two dates.
577
592
  *
@@ -590,20 +605,10 @@ const endOfMonthUtil = (date, timeZone) => {
590
605
  * const fromDate = new Date(dateString1);
591
606
  * const toDate = new Date(dateString2);
592
607
  * const result = differenceInSecondsUtil(fromDate, toDate);
593
- * Output: 5
608
+ * Output: 300
594
609
  */
595
610
  const differenceInSecondsUtil = (from, to) => {
596
- let until = new Temporal.Duration();
597
- try {
598
- until = toZonedDateTimeUtil(from).toPlainTime().until(toZonedDateTimeUtil(to).toPlainTime(), {
599
- largestUnit: "seconds",
600
- });
601
- }
602
- catch (error) {
603
- // eslint-disable-next-line no-console
604
- console.warn("Error: ", error);
605
- }
606
- return until.seconds;
611
+ return differenceInZonedUnit(from, to, "seconds");
607
612
  };
608
613
  /**
609
614
  * Calculates the difference in minutes between two dates.
@@ -626,17 +631,7 @@ const differenceInSecondsUtil = (from, to) => {
626
631
  * Output: 5
627
632
  */
628
633
  const differenceInMinutesUtil = (from, to) => {
629
- let until = new Temporal.Duration();
630
- try {
631
- until = toZonedDateTimeUtil(from).toPlainTime().until(toZonedDateTimeUtil(to).toPlainTime(), {
632
- largestUnit: "minutes",
633
- });
634
- }
635
- catch (error) {
636
- // eslint-disable-next-line no-console
637
- console.warn("Error: ", error);
638
- }
639
- return until.minutes;
634
+ return differenceInZonedUnit(from, to, "minutes");
640
635
  };
641
636
  /**
642
637
  * Calculates the difference in hours between two dates.
@@ -660,17 +655,7 @@ const differenceInMinutesUtil = (from, to) => {
660
655
  * Output: 2
661
656
  */
662
657
  const differenceInHoursUtil = (from, to) => {
663
- let until = new Temporal.Duration();
664
- try {
665
- until = toZonedDateTimeUtil(from).toPlainTime().until(toZonedDateTimeUtil(to).toPlainTime(), {
666
- largestUnit: "hours",
667
- });
668
- }
669
- catch (error) {
670
- // eslint-disable-next-line no-console
671
- console.warn("Error: ", error);
672
- }
673
- return until.hours;
658
+ return differenceInZonedUnit(from, to, "hours");
674
659
  };
675
660
  /**
676
661
  * Calculates the difference in days between two dates.
@@ -896,7 +881,7 @@ const isSameDayUtil = (date, compareTo) => {
896
881
  const isSameWeekUtil = (date) => {
897
882
  const today = toZonedDateTimeUtil(Temporal.Now.instant());
898
883
  const input = toZonedDateTimeUtil(date);
899
- return today.weekOfYear === input.weekOfYear && today.year === input.year;
884
+ return today.weekOfYear === input.weekOfYear && today.yearOfWeek === input.yearOfWeek;
900
885
  };
901
886
  /**
902
887
  * Checks if a date is same month as current.
@@ -1226,11 +1211,6 @@ const timeSinceInDays = (from, to, timeZone, locale, style) => {
1226
1211
  * @param {string} [timeZone] - The time zone to use for the calculations. Defaults to the local time zone.
1227
1212
  * @param {string} [locale] - The locale to use for formatting the duration. Defaults to the user's locale.
1228
1213
  * @returns {string} - The formatted duration in months.
1229
- * @remarks When both `from` and `to` fall in a leap year, the function falls back to
1230
- * `timeSinceInDays` and returns the raw day count for the whole span. Callers relying on a
1231
- * specific unit — especially with `style="narrow-compact"`, where the unit is visible —
1232
- * should be aware that the output can be an arbitrarily large day count (e.g. `"60d"` for a
1233
- * two-month gap) instead of months on those inputs.
1234
1214
  * @example
1235
1215
  * const startDate = "2023-05-10"
1236
1216
  * const endDate = "2023-07-11"
@@ -1239,10 +1219,6 @@ const timeSinceInDays = (from, to, timeZone, locale, style) => {
1239
1219
  * Output: "2 months ago"
1240
1220
  */
1241
1221
  const timeSinceInMonths = (from, to, timeZone, locale, style) => {
1242
- const isLeapYear = toZonedDateTimeUtil(from, timeZone).inLeapYear && toZonedDateTimeUtil(to, timeZone).inLeapYear;
1243
- if (isLeapYear) {
1244
- return timeSinceInDays(from, to, timeZone, locale, style);
1245
- }
1246
1222
  let since = new Temporal.Duration();
1247
1223
  try {
1248
1224
  since = toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
@@ -1264,11 +1240,6 @@ const timeSinceInMonths = (from, to, timeZone, locale, style) => {
1264
1240
  * @param {string} [timeZone] - The time zone to use for the calculations. Defaults to the local time zone.
1265
1241
  * @param {string} [locale] - The locale to use for formatting the duration. Defaults to the user's locale.
1266
1242
  * @returns {string} - The formatted duration in years.
1267
- * @remarks When both `from` and `to` fall in a leap year, the function falls back to
1268
- * `timeSinceInDays` and returns the raw day count for the whole span. Callers relying on a
1269
- * specific unit — especially with `style="narrow-compact"`, where the unit is visible —
1270
- * should be aware that the output can be an arbitrarily large day count (e.g. `"1462d"` for
1271
- * a ~4-year gap between two leap-year endpoints) instead of years on those inputs.
1272
1243
  * @example
1273
1244
  * const startDate = "2023-05-10"
1274
1245
  * const endDate = "2024-05-11"
@@ -1277,10 +1248,6 @@ const timeSinceInMonths = (from, to, timeZone, locale, style) => {
1277
1248
  * Output: "1 year ago"
1278
1249
  */
1279
1250
  const timeSinceInYears = (from, to, timeZone, locale, style) => {
1280
- const isLeapYear = toZonedDateTimeUtil(from, timeZone).inLeapYear && toZonedDateTimeUtil(to, timeZone).inLeapYear;
1281
- if (isLeapYear) {
1282
- return timeSinceInDays(from, to, timeZone, locale, style);
1283
- }
1284
1251
  let since = new Temporal.Duration();
1285
1252
  try {
1286
1253
  since = toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
@@ -1658,11 +1625,15 @@ const getWeekUtil = (date, options) => {
1658
1625
  const _date = convertToPlainDate(date);
1659
1626
  const { weekStartsOn = 0, firstWeekContainsDate = 1 } = options || {};
1660
1627
  const startOfCurrentWeek = startOfWeek(_date, weekStartsOn);
1661
- const startOfWeekYear = startOfWeek(Temporal.PlainDate.from({
1662
- year: _date.year,
1663
- month: 1,
1664
- day: firstWeekContainsDate,
1665
- }), weekStartsOn);
1628
+ const getStartOfWeekYear = (year) => startOfWeek(Temporal.PlainDate.from({ year, month: 1, day: firstWeekContainsDate }), weekStartsOn);
1629
+ const startOfNextWeekYear = getStartOfWeekYear(_date.year + 1);
1630
+ if (Temporal.PlainDate.compare(_date, startOfNextWeekYear) >= 0) {
1631
+ return 1;
1632
+ }
1633
+ const startOfThisWeekYear = getStartOfWeekYear(_date.year);
1634
+ const startOfWeekYear = Temporal.PlainDate.compare(_date, startOfThisWeekYear) < 0
1635
+ ? getStartOfWeekYear(_date.year - 1)
1636
+ : startOfThisWeekYear;
1666
1637
  const diffInDays = startOfCurrentWeek.since(startOfWeekYear).total({ unit: "days" });
1667
1638
  return Math.floor(diffInDays / 7) + 1;
1668
1639
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/date-and-time-utils",
3
- "version": "1.20.52",
3
+ "version": "1.20.54",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
@@ -444,7 +444,7 @@ export declare const endOfMonthUtil: (date: TemporalDate, timeZone?: string) =>
444
444
  * const fromDate = new Date(dateString1);
445
445
  * const toDate = new Date(dateString2);
446
446
  * const result = differenceInSecondsUtil(fromDate, toDate);
447
- * Output: 5
447
+ * Output: 300
448
448
  */
449
449
  export declare const differenceInSecondsUtil: (from: TemporalDate, to: TemporalDate) => number;
450
450
  /**
@@ -838,11 +838,6 @@ export declare const timeSinceInDays: (from: TemporalDate, to: TemporalDate, tim
838
838
  * @param {string} [timeZone] - The time zone to use for the calculations. Defaults to the local time zone.
839
839
  * @param {string} [locale] - The locale to use for formatting the duration. Defaults to the user's locale.
840
840
  * @returns {string} - The formatted duration in months.
841
- * @remarks When both `from` and `to` fall in a leap year, the function falls back to
842
- * `timeSinceInDays` and returns the raw day count for the whole span. Callers relying on a
843
- * specific unit — especially with `style="narrow-compact"`, where the unit is visible —
844
- * should be aware that the output can be an arbitrarily large day count (e.g. `"60d"` for a
845
- * two-month gap) instead of months on those inputs.
846
841
  * @example
847
842
  * const startDate = "2023-05-10"
848
843
  * const endDate = "2023-07-11"
@@ -859,11 +854,6 @@ export declare const timeSinceInMonths: (from: TemporalDate, to: TemporalDate, t
859
854
  * @param {string} [timeZone] - The time zone to use for the calculations. Defaults to the local time zone.
860
855
  * @param {string} [locale] - The locale to use for formatting the duration. Defaults to the user's locale.
861
856
  * @returns {string} - The formatted duration in years.
862
- * @remarks When both `from` and `to` fall in a leap year, the function falls back to
863
- * `timeSinceInDays` and returns the raw day count for the whole span. Callers relying on a
864
- * specific unit — especially with `style="narrow-compact"`, where the unit is visible —
865
- * should be aware that the output can be an arbitrarily large day count (e.g. `"1462d"` for
866
- * a ~4-year gap between two leap-year endpoints) instead of years on those inputs.
867
857
  * @example
868
858
  * const startDate = "2023-05-10"
869
859
  * const endDate = "2024-05-11"