@acorex/core 22.1.0-next.8 → 22.1.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.
@@ -76,9 +76,9 @@ class AXDateTime {
76
76
  // 01 02 ... 11 12
77
77
  MM: () => this.pad(this.calendar.monthOfYear(this.date), 2),
78
78
  // Jan Feb ... Nov Dec
79
- MMM: () => translateSync(`@acorex:dateTime.months.${this.calendar.name()}.short.${this.calendar.monthOfYear(this.date) - 1}`),
79
+ MMM: () => translateSync(`dateTime.months.${this.calendar.name()}.short.${this.calendar.monthOfYear(this.date) - 1}`),
80
80
  // January February ... November December
81
- MMMM: () => translateSync(`@acorex:dateTime.months.${this.calendar.name()}.long.${this.calendar.monthOfYear(this.date) - 1}`),
81
+ MMMM: () => translateSync(`dateTime.months.${this.calendar.name()}.long.${this.calendar.monthOfYear(this.date) - 1}`),
82
82
  // 1 2 ... 7
83
83
  E: () => this.calendar.dayOfWeek(this.date).toString(),
84
84
  // 01 02 ... 07
@@ -554,12 +554,24 @@ class AXGregorianCalendar extends AXCalendar {
554
554
  break;
555
555
  case 'week':
556
556
  break;
557
- case 'month':
558
- clone.setMonth(Math.max(0, value - 1));
557
+ case 'month': {
558
+ const newMonth = Math.max(0, value - 1);
559
+ const lastDayOfMonth = new Date(clone.getFullYear(), newMonth + 1, 0).getDate();
560
+ const day = Math.min(clone.getDate(), lastDayOfMonth);
561
+ // Set to the 1st first so native Date does not overflow (e.g. Jan 31 → Feb).
562
+ clone.setDate(1);
563
+ clone.setMonth(newMonth);
564
+ clone.setDate(day);
559
565
  break;
560
- case 'year':
566
+ }
567
+ case 'year': {
568
+ const isFeb29 = clone.getMonth() === 1 && clone.getDate() === 29;
561
569
  clone.setFullYear(value);
570
+ if (isFeb29 && clone.getMonth() !== 1) {
571
+ clone.setMonth(1, 28);
572
+ }
562
573
  break;
574
+ }
563
575
  }
564
576
  return new AXDateTime(clone, this);
565
577
  }
@@ -778,10 +790,7 @@ class AXSolarHijriCalendar extends AXCalendar {
778
790
  default:
779
791
  case 'day': {
780
792
  const gDate = this.toGregorian(jDate.year, jDate.month, value);
781
- clone.setFullYear(gDate.getFullYear());
782
- clone.setMonth(gDate.getMonth());
783
- clone.setDate(gDate.getDate());
784
- break;
793
+ return this.withCivilDate(clone, gDate);
785
794
  }
786
795
  case 'week':
787
796
  break;
@@ -789,19 +798,24 @@ class AXSolarHijriCalendar extends AXCalendar {
789
798
  const lastDay = this.SolarHijriMonthLength(jDate.year, value);
790
799
  const day = Math.min(jDate.day, lastDay);
791
800
  const gDate2 = this.toGregorian(jDate.year, value, day);
792
- clone.setFullYear(gDate2.getFullYear());
793
- clone.setMonth(gDate2.getMonth());
794
- clone.setDate(gDate2.getDate());
795
- break;
801
+ return this.withCivilDate(clone, gDate2);
796
802
  }
797
803
  case 'year': {
798
- const gDate3 = this.toGregorian(value, jDate.month, jDate.day);
799
- clone.setFullYear(gDate3.getFullYear());
800
- break;
804
+ const lastDay = this.SolarHijriMonthLength(value, jDate.month);
805
+ const day = Math.min(jDate.day, lastDay);
806
+ const gDate3 = this.toGregorian(value, jDate.month, day);
807
+ return this.withCivilDate(clone, gDate3);
801
808
  }
802
809
  }
803
810
  return new AXDateTime(clone, this);
804
811
  }
812
+ /**
813
+ * Builds a Date from converted civil Y/M/D while keeping the original time.
814
+ * Assigning year/month/day on an existing Date sequentially overflows (e.g. Aug 31 → Feb).
815
+ */
816
+ withCivilDate(source, civil) {
817
+ return new AXDateTime(new Date(civil.getFullYear(), civil.getMonth(), civil.getDate(), source.getHours(), source.getMinutes(), source.getSeconds(), source.getMilliseconds()), this);
818
+ }
805
819
  startOf(date, unit) {
806
820
  const clone = new Date(date.valueOf());
807
821
  switch (unit) {