@fr0st/datetime 5.0.2 → 5.1.1

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/README.md CHANGED
@@ -44,7 +44,7 @@ import DateTime from '@fr0st/datetime';
44
44
  ## Date Creation
45
45
 
46
46
  - `dateString` is a string representing the date, and will default to the current timestamp.
47
- - `options` is an object containing properties to define the new date.
47
+ - `options` is an object containing options for creating the new date.
48
48
  - `timeZone` is a string representing the time zone of the date, and will default to the system time zone.
49
49
  - `locale` is a string representing the locale of the date, and will default to the system locale.
50
50
 
@@ -55,7 +55,7 @@ const dateTime = new DateTime(dateString, options);
55
55
  **From Array**
56
56
 
57
57
  - `dateArray` is an array containing the year, month, date, hours, minutes, seconds and milliseconds.
58
- - `options` is an object containing properties to define the new date.
58
+ - `options` is an object containing options for creating the new date.
59
59
  - `timeZone` is a string representing the time zone of the date, and will default to the system time zone.
60
60
  - `locale` is a string representing the locale of the date, and will default to the system locale.
61
61
 
@@ -68,7 +68,7 @@ The month and date in the `dateArray` will default to 1 if not set. The hours, m
68
68
  **From Date**
69
69
 
70
70
  - `dateObj` is a native JS *Date* object.
71
- - `options` is an object containing properties to define the new date.
71
+ - `options` is an object containing options for creating the new date.
72
72
  - `timeZone` is a string representing the time zone of the date, and will default to the system time zone.
73
73
  - `locale` is a string representing the locale of the date, and will default to the system locale.
74
74
 
@@ -82,7 +82,7 @@ If you wish to parse a date string and you know the exact format, you can use th
82
82
 
83
83
  - `formatString` is a string containing the format you wish to use for parsing.
84
84
  - `dateString` is a string representing the date you are parsing.
85
- - `options` is an object containing properties to define the new date.
85
+ - `options` is an object containing options for creating the new date.
86
86
  - `timeZone` is a string representing the time zone of the date, and will default to the system time zone.
87
87
  - `locale` is a string representing the locale of the date, and will default to the system locale.
88
88
 
@@ -97,7 +97,7 @@ const dateTime = DateTime.fromFormat(formatString, dateString, options);
97
97
  **From ISO String**
98
98
 
99
99
  - `dateString` is a string representing the date you are parsing.
100
- - `options` is an object containing properties to define the new date.
100
+ - `options` is an object containing options for creating the new date.
101
101
  - `timeZone` is a string representing the time zone of the date, and will default to the system time zone.
102
102
  - `locale` is a string representing the locale of the date, and will default to English.
103
103
 
@@ -114,7 +114,7 @@ const dateTime = DateTime.fromISOString(dateString, options);
114
114
  **From Timestamp**
115
115
 
116
116
  - `timestamp` is the number of seconds since the UNIX epoch.
117
- - `options` is an object containing properties to define the new date.
117
+ - `options` is an object containing options for creating the new date.
118
118
  - `timeZone` is a string representing the time zone of the date, and will default to the system time zone.
119
119
  - `locale` is a string representing the locale of the date, and will default to the system locale.
120
120
 
@@ -124,7 +124,7 @@ const dateTime = DateTime.fromTimestamp(timestamp, options);
124
124
 
125
125
  **Now**
126
126
 
127
- - `options` is an object containing properties to define the new date.
127
+ - `options` is an object containing options for creating the new date.
128
128
  - `timeZone` is a string representing the time zone of the date, and will default to the system time zone.
129
129
  - `locale` is a string representing the locale of the date, and will default to the system locale.
130
130
 
@@ -694,22 +694,23 @@ const daysInYear = dateTime.daysInYear();
694
694
  Get the difference between two Dates.
695
695
 
696
696
  - `other` is the *DateTime* object to compare to.
697
- - `timeUnit` is a string representing the unit of time to return, and can be one of either "*year*", "*month*", "*week*", "*day*", "*hour*", "*minute*" or "*second*", or their pluralized versions.
698
- - `relative` is a boolean indicating whether to return the relative difference, and will default to *true*.
697
+ - `options` is an object containing options for how to compare the dates.
698
+ - `timeUnit` is a string representing the unit of time to return, and can be one of either "*year*", "*month*", "*week*", "*day*", "*hour*", "*minute*" or "*second*", or their pluralized versions.
699
+ - `relative` is a boolean indicating whether to return the relative difference, and will default to *true*.
699
700
 
700
701
  If the `timeUnit` is omitted, this method will return the difference in milliseconds.
701
702
 
702
703
  ```javascript
703
- const diff = dateTime.diff(other, timeUnit, relative);
704
+ const diff = dateTime.diff(other, options);
704
705
  ```
705
706
 
706
707
  If `relative` is *true* (default) the value returned will be the difference in the specified `timeUnit`, ignoring less significant values.
707
708
 
708
709
  ```javascript
709
- DateTime.fromFormat('yyyy-MM-dd', '2019-01-01').diff(DateTime.fromFormat('yyyy-MM-dd', '2018-12-31'), 'years', true); // 1
710
- DateTime.fromFormat('yyyy-MM-dd', '2019-01-01').diff(DateTime.fromFormat('yyyy-MM-dd', '2018-12-31'), 'years', false); // 0
711
- DateTime.fromFormat('yyyy-MM-dd', '2019-01-01').diff(DateTime.fromFormat('yyyy-MM-dd', '2017-12-31'), 'years', true); // 2
712
- DateTime.fromFormat('yyyy-MM-dd', '2019-01-01').diff(DateTime.fromFormat('yyyy-MM-dd', '2017-12-31'), 'years', false); // 1
710
+ DateTime.fromFormat('yyyy-MM-dd', '2019-01-01').diff(DateTime.fromFormat('yyyy-MM-dd', '2018-12-31'), { timeUnit: 'years', relative: true }); // 1
711
+ DateTime.fromFormat('yyyy-MM-dd', '2019-01-01').diff(DateTime.fromFormat('yyyy-MM-dd', '2018-12-31'), { timeUnit: 'years', relative: false }); // 0
712
+ DateTime.fromFormat('yyyy-MM-dd', '2019-01-01').diff(DateTime.fromFormat('yyyy-MM-dd', '2017-12-31'), { timeUnit: 'years', relative: true }); // 2
713
+ DateTime.fromFormat('yyyy-MM-dd', '2019-01-01').diff(DateTime.fromFormat('yyyy-MM-dd', '2017-12-31'), { timeUnit: 'years', relative: false }); // 1
713
714
  ```
714
715
 
715
716
  **Era**
@@ -727,12 +728,13 @@ const era = dateTime.era(type);
727
728
  Get the relative difference between two Dates in a human readable format using the current locale.
728
729
 
729
730
  - `other` is the *DateTime* object to compare to.
730
- - `timeUnit` is a string representing the unit of time to return, and can be one of either "*year*", "*month*", "*week*", "*day*", "*hour*", "*minute*" or "*second*", or their pluralized versions.
731
+ - `options` is an object containing options for how to compare the dates.
732
+ - `timeUnit` is a string representing the unit of time to return, and can be one of either "*year*", "*month*", "*week*", "*day*", "*hour*", "*minute*" or "*second*", or their pluralized versions.
731
733
 
732
734
  If the `timeUnit` is omitted, this method will use the (relative) most significant non-zero value.
733
735
 
734
736
  ```javascript
735
- const diff = dateTime.humanDiff(other, timeUnit);
737
+ const diff = dateTime.humanDiff(other, options);
736
738
  ```
737
739
 
738
740
  The most significant non-zero value is determined where the unit of time has a non-relative difference, or the next relative difference value is greater than or equal to the unit of time.
@@ -751,10 +753,11 @@ DateTime.fromFormat('yyyy-MM-dd', '2019-01-01').humanDiff(DateTime.fromFormat('y
751
753
  Return *true* if the *DateTime* is after another date.
752
754
 
753
755
  - `other` is the *DateTime* object to compare to.
754
- - `granularity` is a string specifying the level of granularity to use when comparing the dates, and can be one of either "*year*", "*month*", "*day*", "*hour*", "*minute*" or "*second*".
756
+ - `options` is an object containing options for how to compare the dates.
757
+ - `granularity` is a string specifying the level of granularity to use when comparing the dates, and can be one of either "*year*", "*month*", "*day*", "*hour*", "*minute*" or "*second*".
755
758
 
756
759
  ```javascript
757
- const isAfter = dateTime.isAfter(other, granularity);
760
+ const isAfter = dateTime.isAfter(other, options);
758
761
  ```
759
762
 
760
763
  If a `granularity` is not specified, this method will compare the dates in milliseconds.
@@ -764,10 +767,11 @@ If a `granularity` is not specified, this method will compare the dates in milli
764
767
  Return *true* if the *DateTime* is before another date.
765
768
 
766
769
  - `other` is the *DateTime* object to compare to.
767
- - `granularity` is a string specifying the level of granularity to use when comparing the dates, and can be one of either "*year*", "*month*", "*day*", "*hour*", "*minute*" or "*second*".
770
+ - `options` is an object containing options for how to compare the dates.
771
+ - `granularity` is a string specifying the level of granularity to use when comparing the dates, and can be one of either "*year*", "*month*", "*day*", "*hour*", "*minute*" or "*second*".
768
772
 
769
773
  ```javascript
770
- const isBefore = dateTime.isBefore(other, granularity);
774
+ const isBefore = dateTime.isBefore(other, options);
771
775
  ```
772
776
 
773
777
  If a `granularity` is not specified, this method will compare the dates in milliseconds.
@@ -778,10 +782,11 @@ Return *true* if the *DateTime* is between two other dates.
778
782
 
779
783
  - `start` is the starting *DateTime* object to compare to.
780
784
  - `end` is the ending *DateTime* object to compare to.
781
- - `granularity` is a string specifying the level of granularity to use when comparing the dates, and can be one of either "*year*", "*month*", "*day*", "*hour*", "*minute*" or "*second*".
785
+ - `options` is an object containing options for how to compare the dates.
786
+ - `granularity` is a string specifying the level of granularity to use when comparing the dates, and can be one of either "*year*", "*month*", "*day*", "*hour*", "*minute*" or "*second*".
782
787
 
783
788
  ```javascript
784
- const isBetween = dateTime.isBetween(start, end, granularity);
789
+ const isBetween = dateTime.isBetween(start, end, options);
785
790
  ```
786
791
 
787
792
  If a `granularity` is not specified, this method will compare the dates in milliseconds.
@@ -807,10 +812,11 @@ const isLeapYear = dateTime.isLeapYear();
807
812
  Return *true* if the *DateTime* is the same as another date.
808
813
 
809
814
  - `other` is the *DateTime* object to compare to.
810
- - `granularity` is a string specifying the level of granularity to use when comparing the dates, and can be one of either "*year*", "*month*", "*day*", "*hour*", "*minute*" or "*second*".
815
+ - `options` is an object containing options for how to compare the dates.
816
+ - `granularity` is a string specifying the level of granularity to use when comparing the dates, and can be one of either "*year*", "*month*", "*day*", "*hour*", "*minute*" or "*second*".
811
817
 
812
818
  ```javascript
813
- const isSame = dateTime.isSame(other, granularity);
819
+ const isSame = dateTime.isSame(other, options);
814
820
  ```
815
821
 
816
822
  If a `granularity` is not specified, this method will compare the dates in milliseconds.
@@ -820,10 +826,11 @@ If a `granularity` is not specified, this method will compare the dates in milli
820
826
  Return *true* if the *DateTime* is the same or after another date.
821
827
 
822
828
  - `other` is the *DateTime* object to compare to.
823
- - `granularity` is a string specifying the level of granularity to use when comparing the dates, and can be one of either "*year*", "*month*", "*day*", "*hour*", "*minute*" or "*second*".
829
+ - `options` is an object containing options for how to compare the dates.
830
+ - `granularity` is a string specifying the level of granularity to use when comparing the dates, and can be one of either "*year*", "*month*", "*day*", "*hour*", "*minute*" or "*second*".
824
831
 
825
832
  ```javascript
826
- const isSameOrAfter = dateTime.isSameOrAfter(other, granularity);
833
+ const isSameOrAfter = dateTime.isSameOrAfter(other, options);
827
834
  ```
828
835
 
829
836
  If a `granularity` is not specified, this method will compare the dates in milliseconds.
@@ -833,10 +840,11 @@ If a `granularity` is not specified, this method will compare the dates in milli
833
840
  Return *true* if the *DateTime* is the same or before another date.
834
841
 
835
842
  - `other` is the *DateTime* object to compare to.
836
- - `granularity` is a string specifying the level of granularity to use when comparing the dates, and can be one of either "*year*", "*month*", "*day*", "*hour*", "*minute*" or "*second*".
843
+ - `options` is an object containing options for how to compare the dates.
844
+ - `granularity` is a string specifying the level of granularity to use when comparing the dates, and can be one of either "*year*", "*month*", "*day*", "*hour*", "*minute*" or "*second*".
837
845
 
838
846
  ```javascript
839
- const isSameOrBefore = dateTime.isSameOrBefore(other, granularity);
847
+ const isSameOrBefore = dateTime.isSameOrBefore(other, options);
840
848
  ```
841
849
 
842
850
  If a `granularity` is not specified, this method will compare the dates in milliseconds.
@@ -910,15 +918,15 @@ const daysInYear = DateTime.daysInYear(year);
910
918
  Get the default locale.
911
919
 
912
920
  ```javascript
913
- locale = DateTime.getDefaultLocale();
921
+ const locale = DateTime.getDefaultLocale();
914
922
  ```
915
923
 
916
- **Set Default Time Zone**
924
+ **Get Default Time Zone**
917
925
 
918
926
  Get the default time zone.
919
927
 
920
928
  ```javascript
921
- timeZone = DateTime.getDefaultTimeZone();
929
+ const timeZone = DateTime.getDefaultTimeZone();
922
930
  ```
923
931
 
924
932
  **Is Leap Year?**
@@ -161,12 +161,12 @@
161
161
  function getBiggestDiff(date, other) {
162
162
  let lastResult;
163
163
  for (const timeUnit of ['year', 'month', 'week', 'day', 'hour', 'minute', 'second']) {
164
- const relativeDiff = date.diff(other, timeUnit);
164
+ const relativeDiff = date.diff(other, { timeUnit });
165
165
  if (lastResult && thresholds[timeUnit] && Math.abs(relativeDiff) >= thresholds[timeUnit]) {
166
166
  return lastResult;
167
167
  }
168
168
 
169
- const actualDiff = date.diff(other, timeUnit, false);
169
+ const actualDiff = date.diff(other, { timeUnit, relative: false });
170
170
  if (actualDiff) {
171
171
  return [relativeDiff, timeUnit];
172
172
  }
@@ -2553,11 +2553,12 @@
2553
2553
  /**
2554
2554
  * Get the difference between this and another Date.
2555
2555
  * @param {DateTime} [other] The date to compare to.
2556
- * @param {string} [timeUnit] The unit of time.
2557
- * @param {Boolean} [relative=true] Whether to use the relative difference.
2556
+ * @param {object} [options] The options for comparing the dates.
2557
+ * @param {string} [options.timeUnit] The unit of time.
2558
+ * @param {Boolean} [options.relative=true] Whether to use the relative difference.
2558
2559
  * @return {number} The difference.
2559
2560
  */
2560
- function diff(other, timeUnit, relative = true) {
2561
+ function diff(other, { timeUnit, relative = true } = {}) {
2561
2562
  if (!other) {
2562
2563
  other = new this.constructor;
2563
2564
  }
@@ -2695,10 +2696,11 @@
2695
2696
  /**
2696
2697
  * Get the difference between this and another Date in human readable form.
2697
2698
  * @param {DateTime} [other] The date to compare to.
2698
- * @param {string} [timeUnit] The unit of time.
2699
+ * @param {object} [options] The options for comparing the dates.
2700
+ * @param {string} [options.timeUnit] The unit of time.
2699
2701
  * @return {string} The difference in human readable form.
2700
2702
  */
2701
- function humanDiff(other, timeUnit) {
2703
+ function humanDiff(other, { timeUnit } = {}) {
2702
2704
  const relativeFormatter = getRelativeFormatter(this.getLocale());
2703
2705
 
2704
2706
  if (!relativeFormatter) {
@@ -2711,7 +2713,7 @@
2711
2713
 
2712
2714
  let amount;
2713
2715
  if (timeUnit) {
2714
- amount = this.diff(other, timeUnit);
2716
+ amount = this.diff(other, { timeUnit });
2715
2717
  } else {
2716
2718
  [amount, timeUnit] = getBiggestDiff(this, other);
2717
2719
  }
@@ -2721,30 +2723,33 @@
2721
2723
  /**
2722
2724
  * Determine whether this DateTime is after another date (optionally to a granularity).
2723
2725
  * @param {DateTime} [other] The date to compare to.
2724
- * @param {string} [granularity] The level of granularity to use for comparison.
2726
+ * @param {object} [options] The options for comparing the dates.
2727
+ * @param {string} [options.granularity] The level of granularity to use for comparison.
2725
2728
  * @return {Boolean} TRUE if this DateTime is after the other date, otherwise FALSE.
2726
2729
  */
2727
- function isAfter(other, granularity) {
2728
- return this.diff(other, granularity) > 0;
2730
+ function isAfter(other, { granularity } = {}) {
2731
+ return this.diff(other, { timeUnit: granularity }) > 0;
2729
2732
  }
2730
2733
  /**
2731
2734
  * Determine whether this DateTime is before another date (optionally to a granularity).
2732
2735
  * @param {DateTime} [other] The date to compare to.
2733
- * @param {string} [granularity] The level of granularity to use for comparison.
2736
+ * @param {object} [options] The options for comparing the dates.
2737
+ * @param {string} [options.granularity] The level of granularity to use for comparison.
2734
2738
  * @return {Boolean} TRUE if this DateTime is before the other date, otherwise FALSE.
2735
2739
  */
2736
- function isBefore(other, granularity) {
2737
- return this.diff(other, granularity) < 0;
2740
+ function isBefore(other, { granularity } = {}) {
2741
+ return this.diff(other, { timeUnit: granularity }) < 0;
2738
2742
  }
2739
2743
  /**
2740
2744
  * Determine whether this DateTime is between two other dates (optionally to a granularity).
2741
2745
  * @param {DateTime} [start] The first date to compare to.
2742
2746
  * @param {DateTime} [end] The second date to compare to.
2743
- * @param {string} [granularity] The level of granularity to use for comparison.
2747
+ * @param {object} [options] The options for comparing the dates.
2748
+ * @param {string} [options.granularity] The level of granularity to use for comparison.
2744
2749
  * @return {Boolean} TRUE if this DateTime is between the other dates, otherwise FALSE.
2745
2750
  */
2746
- function isBetween(start, end, granularity) {
2747
- return this.diff(start, granularity) > 0 && this.diff(end, granularity) < 0;
2751
+ function isBetween(start, end, { granularity } = {}) {
2752
+ return this.diff(start, { timeUnit: granularity }) > 0 && this.diff(end, { timeUnit: granularity }) < 0;
2748
2753
  }
2749
2754
  /**
2750
2755
  * Return true if the DateTime is in daylight savings.
@@ -2777,29 +2782,32 @@
2777
2782
  /**
2778
2783
  * Determine whether this DateTime is the same as another date (optionally to a granularity).
2779
2784
  * @param {DateTime} [other] The date to compare to.
2780
- * @param {string} [granularity] The level of granularity to use for comparison.
2785
+ * @param {object} [options] The options for comparing the dates.
2786
+ * @param {string} [options.granularity] The level of granularity to use for comparison.
2781
2787
  * @return {Boolean} TRUE if this DateTime is the same as the other date, otherwise FALSE.
2782
2788
  */
2783
- function isSame(other, granularity) {
2784
- return this.diff(other, granularity) === 0;
2789
+ function isSame(other, { granularity } = {}) {
2790
+ return this.diff(other, { timeUnit: granularity }) === 0;
2785
2791
  }
2786
2792
  /**
2787
2793
  * Determine whether this DateTime is the same or after another date (optionally to a granularity).
2788
2794
  * @param {DateTime} [other] The date to compare to.
2789
- * @param {string} [granularity] The level of granularity to use for comparison.
2795
+ * @param {object} [options] The options for comparing the dates.
2796
+ * @param {string} [options.granularity] The level of granularity to use for comparison.
2790
2797
  * @return {Boolean} TRUE if this DateTime is the same or after the other date, otherwise FALSE.
2791
2798
  */
2792
- function isSameOrAfter(other, granularity) {
2793
- return this.diff(other, granularity) >= 0;
2799
+ function isSameOrAfter(other, { granularity } = {}) {
2800
+ return this.diff(other, { timeUnit: granularity }) >= 0;
2794
2801
  }
2795
2802
  /**
2796
2803
  * Determine whether this DateTime is the same or before another date.
2797
2804
  * @param {DateTime} other The date to compare to.
2798
- * @param {string} [granularity] The level of granularity to use for comparison.
2805
+ * @param {object} [options] The options for comparing the dates.
2806
+ * @param {string} [options.granularity] The level of granularity to use for comparison.
2799
2807
  * @return {Boolean} TRUE if this DateTime is the same or before the other date, otherwise FALSE.
2800
2808
  */
2801
- function isSameOrBefore(other, granularity) {
2802
- return this.diff(other, granularity) <= 0;
2809
+ function isSameOrBefore(other, { granularity } = {}) {
2810
+ return this.diff(other, { timeUnit: granularity }) <= 0;
2803
2811
  }
2804
2812
  /**
2805
2813
  * Get the name of the month in current timeZone.