@formkit/tempo 1.3.0 → 1.4.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/dist/bundle.d.ts CHANGED
@@ -748,19 +748,19 @@ declare function diffDays(dateA: DateInput, dateB?: MaybeDateInput, roundingMeth
748
748
  declare function diffDays(dateA: MaybeDateInput, dateB: DateInput, roundingMethod?: DiffRoundingMethod): number;
749
749
 
750
750
  /**
751
- * Returns the difference between 2 dates in days.
751
+ * Returns the difference between 2 dates in weeks.
752
752
  * @param dateA - A date to compare with the right date
753
753
  * @param [dateB] - A date to compare with the left date or nothing to compare with the current time
754
754
  * @param [roundingMethod] - the rounding method to use, default: trunc
755
755
  */
756
- declare function diffWeeks(dateA: DateInput, dateB: DateInput, roundingMethod?: DiffRoundingMethod): number;
756
+ declare function diffWeeks(dateA: DateInput, dateB?: MaybeDateInput, roundingMethod?: DiffRoundingMethod): number;
757
757
  /**
758
- * Returns the difference between 2 dates in days.
758
+ * Returns the difference between 2 dates in weeks.
759
759
  * @param [dateA] - A date to compare with the right date or null to compare with the current time
760
760
  * @param dateB - A date to compare with the left date
761
761
  * @param [roundingMethod] - the rounding method to use, default: trunc
762
762
  */
763
- declare function diffWeeks(dateA: DateInput, dateB: DateInput, roundingMethod?: DiffRoundingMethod): number;
763
+ declare function diffWeeks(dateA: MaybeDateInput, dateB: DateInput, roundingMethod?: DiffRoundingMethod): number;
764
764
 
765
765
  /**
766
766
  * Returns the difference between 2 dates in months.
package/dist/bundle.mjs CHANGED
@@ -482,9 +482,7 @@ function tzDate(inputDate, tz) {
482
482
  // src/dayOfYear.ts
483
483
  function dayOfYear(inputDate) {
484
484
  const d = date(inputDate);
485
- return Math.round(
486
- (new Date(d.getFullYear(), d.getMonth(), d.getDate(), 0, 0).getTime() - new Date(d.getFullYear(), 0, 0).getTime()) / 864e5
487
- );
485
+ return (Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()) - Date.UTC(d.getFullYear(), 0, 0)) / 864e5;
488
486
  }
489
487
 
490
488
  // src/dayEnd.ts
@@ -762,6 +760,11 @@ function fourDigitYear(value) {
762
760
  // src/hourEnd.ts
763
761
  function hourEnd(inputDate) {
764
762
  const d = date(inputDate);
763
+ const shifted = new Date(
764
+ d.getTime() + ((59 - d.getMinutes()) * 60 + (59 - d.getSeconds())) * 1e3 + (999 - d.getMilliseconds())
765
+ );
766
+ if (shifted.getMinutes() === 59 && shifted.getHours() === d.getHours())
767
+ return shifted;
765
768
  d.setMinutes(59, 59, 999);
766
769
  return d;
767
770
  }
@@ -769,6 +772,10 @@ function hourEnd(inputDate) {
769
772
  // src/hourStart.ts
770
773
  function hourStart(inputDate) {
771
774
  const d = date(inputDate);
775
+ const shifted = new Date(
776
+ d.getTime() - (d.getMinutes() * 60 + d.getSeconds()) * 1e3 - d.getMilliseconds()
777
+ );
778
+ if (shifted.getMinutes() === 0 && shifted.getHours() === d.getHours()) return shifted;
772
779
  d.setMinutes(0, 0, 0);
773
780
  return d;
774
781
  }
@@ -776,14 +783,16 @@ function hourStart(inputDate) {
776
783
  // src/minuteEnd.ts
777
784
  function minuteEnd(inputDate) {
778
785
  const d = date(inputDate);
779
- d.setSeconds(59, 999);
786
+ d.setTime(
787
+ d.getTime() + (59 - d.getSeconds()) * 1e3 + (999 - d.getMilliseconds())
788
+ );
780
789
  return d;
781
790
  }
782
791
 
783
792
  // src/minuteStart.ts
784
793
  function minuteStart(inputDate) {
785
794
  const d = date(inputDate);
786
- d.setSeconds(0, 0);
795
+ d.setTime(d.getTime() - d.getSeconds() * 1e3 - d.getMilliseconds());
787
796
  return d;
788
797
  }
789
798
 
@@ -808,23 +817,23 @@ function nearestDay(inputDate, search, constraint = 7) {
808
817
  const d = date(inputDate);
809
818
  switch (constraint) {
810
819
  case "month":
811
- decrements = d.getDate();
820
+ decrements = d.getDate() - 1;
812
821
  increments = monthDays(d) - d.getDate();
813
822
  break;
814
823
  case "week":
815
- decrements = d.getDay() + 1;
824
+ decrements = d.getDay();
816
825
  increments = 6 - d.getDay();
817
826
  break;
818
827
  case "year":
819
828
  const total = yearDays(d);
820
829
  const day = dayOfYear(d);
821
- decrements = day;
830
+ decrements = day - 1;
822
831
  increments = total - day;
823
832
  break;
824
833
  default:
825
834
  increments = decrements = constraint;
826
835
  }
827
- for (let i = 0; i <= increments || i < decrements; i++) {
836
+ for (let i = 0; i <= increments || i <= decrements; i++) {
828
837
  if (i <= increments) {
829
838
  const next = addDay(d, i);
830
839
  if (search(next)) return next;
@@ -1265,24 +1274,24 @@ function diffHours(dateA, dateB, roundingMethod) {
1265
1274
 
1266
1275
  // src/diffDays.ts
1267
1276
  function diffDays(dateA, dateB, roundingMethod) {
1268
- return diffRound(
1269
- // @ts-ignore
1270
- diffMilliseconds(dateA, dateB) / 864e5,
1271
- // hour * 24
1272
- roundingMethod
1273
- );
1277
+ const left = date(dateA);
1278
+ const right = date(dateB);
1279
+ const offset2 = (right.getTimezoneOffset() - left.getTimezoneOffset()) * 6e4;
1280
+ return diffRound((+left - +right + offset2) / 864e5, roundingMethod);
1274
1281
  }
1275
1282
 
1276
1283
  // src/diffWeeks.ts
1277
1284
  function diffWeeks(dateA, dateB, roundingMethod) {
1278
- return diffRound(
1279
- diffMilliseconds(dateA, dateB) / 6048e5,
1280
- // day * 7
1281
- roundingMethod
1282
- );
1285
+ const left = date(dateA);
1286
+ const right = date(dateB);
1287
+ const offset2 = (right.getTimezoneOffset() - left.getTimezoneOffset()) * 6e4;
1288
+ return diffRound((+left - +right + offset2) / 6048e5, roundingMethod);
1283
1289
  }
1284
1290
 
1285
1291
  // src/diffMonths.ts
1292
+ function timeOfDay(d) {
1293
+ return ((d.getHours() * 60 + d.getMinutes()) * 60 + d.getSeconds()) * 1e3 + d.getMilliseconds();
1294
+ }
1286
1295
  function diffMonths(dateA, dateB) {
1287
1296
  const l = date(dateA);
1288
1297
  const r = date(dateB);
@@ -1293,7 +1302,7 @@ function diffMonths(dateA, dateB) {
1293
1302
  let months = (l.getFullYear() - r.getFullYear()) * 12 + (l.getMonth() - r.getMonth());
1294
1303
  const ld = l.getDate();
1295
1304
  const rd = r.getDate();
1296
- if (ld < rd) {
1305
+ if (ld < rd || ld == rd && timeOfDay(l) < timeOfDay(r)) {
1297
1306
  const lm = monthDays(l);
1298
1307
  if (!(lm == ld && lm < rd)) {
1299
1308
  months--;