@fr0st/datetime 5.1.6 → 6.0.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.
@@ -1,4 +1,3 @@
1
- import { modify } from './../helpers.js';
2
1
  import { daysInMonth } from './../static/utility.js';
3
2
 
4
3
  /**
@@ -6,91 +5,391 @@ import { daysInMonth } from './../static/utility.js';
6
5
  */
7
6
 
8
7
  /**
9
- * Add a duration to the date.
10
- * @param {number} amount The amount to modify the date by.
11
- * @param {string} timeUnit The unit of time.
8
+ * Add a day to the current DateTime.
12
9
  * @return {DateTime} The DateTime object.
13
10
  */
14
- export function add(amount, timeUnit) {
15
- return modify(this, amount, timeUnit);
11
+ export function addDay() {
12
+ return this.addDays(1);
16
13
  };
17
14
 
18
15
  /**
19
- * Modify the DateTime by setting it to the end of a unit of time.
20
- * @param {string} timeUnit The unit of time.
16
+ * Add days to the current DateTime.
17
+ * @param {number} amount The number of days to add.
21
18
  * @return {DateTime} The DateTime object.
22
19
  */
23
- export function endOf(timeUnit) {
24
- timeUnit = timeUnit.toLowerCase();
20
+ export function addDays(amount) {
21
+ return this.setDate(
22
+ this.getDate() + amount,
23
+ );
24
+ };
25
+
26
+ /**
27
+ * Add an hour to the current DateTime.
28
+ * @return {DateTime} The DateTime object.
29
+ */
30
+ export function addHour() {
31
+ return this.addHours(1);
32
+ };
33
+
34
+ /**
35
+ * Add hours to the current DateTime.
36
+ * @param {number} amount The number of hours to add.
37
+ * @return {DateTime} The DateTime object.
38
+ */
39
+ export function addHours(amount) {
40
+ return this.setTime(
41
+ this.getTime() + (amount * 3600000),
42
+ );
43
+ };
44
+
45
+ /**
46
+ * Add a minute to the current DateTime.
47
+ * @return {DateTime} The DateTime object.
48
+ */
49
+ export function addMinute() {
50
+ return this.addMinutes(1);
51
+ };
52
+
53
+ /**
54
+ * Add minutes to the current DateTime.
55
+ * @param {number} amount The number of minutes to add.
56
+ * @return {DateTime} The DateTime object.
57
+ */
58
+ export function addMinutes(amount) {
59
+ return this.setTime(
60
+ this.getTime() + (amount * 60000),
61
+ );
62
+ };
63
+
64
+ /**
65
+ * Add a month to the current DateTime.
66
+ * @return {DateTime} The DateTime object.
67
+ */
68
+ export function addMonth() {
69
+ return this.addMonths(1);
70
+ };
71
+
72
+ /**
73
+ * Add months to the current DateTime.
74
+ * @param {number} amount The number of months to add.
75
+ * @return {DateTime} The DateTime object.
76
+ */
77
+ export function addMonths(amount) {
78
+ return this.setMonth(
79
+ this.getMonth() + amount,
80
+ );
81
+ };
82
+
83
+ /**
84
+ * Add a second to the current DateTime.
85
+ * @return {DateTime} The DateTime object.
86
+ */
87
+ export function addSecond() {
88
+ return this.addSeconds(1);
89
+ };
90
+
91
+ /**
92
+ * Add seconds to the current DateTime.
93
+ * @param {number} amount The number of seconds to add.
94
+ * @return {DateTime} The DateTime object.
95
+ */
96
+ export function addSeconds(amount) {
97
+ return this.setTime(
98
+ this.getTime() + (amount * 1000),
99
+ );
100
+ };
101
+
102
+ /**
103
+ * Add a week to the current DateTime.
104
+ * @return {DateTime} The DateTime object.
105
+ */
106
+ export function addWeek() {
107
+ return this.addWeeks(1);
108
+ };
109
+
110
+ /**
111
+ * Add weeks to the current DateTime.
112
+ * @param {number} amount The number of weeks to add.
113
+ * @return {DateTime} The DateTime object.
114
+ */
115
+ export function addWeeks(amount) {
116
+ return this.setDate(
117
+ this.getDate() + (amount * 7),
118
+ );
119
+ };
120
+
121
+ /**
122
+ * Add a year to the current DateTime.
123
+ * @return {DateTime} The DateTime object.
124
+ */
125
+ export function addYear() {
126
+ return this.addYears(1);
127
+ };
128
+
129
+ /**
130
+ * Add years to the current DateTime.
131
+ * @param {number} amount The number of years to add.
132
+ * @return {DateTime} The DateTime object.
133
+ */
134
+ export function addYears(amount) {
135
+ return this.setYear(
136
+ this.getYear() + amount,
137
+ );
138
+ };
139
+
140
+ /**
141
+ * Set the DateTime to the end of the day.
142
+ * @return {DateTime} The DateTime object.
143
+ */
144
+ export function endOfDay() {
145
+ return this.setHours(23, 59, 59, 999);
146
+ };
147
+
148
+ /**
149
+ * Set the DateTime to the end of the hour.
150
+ * @return {DateTime} The DateTime object.
151
+ */
152
+ export function endOfHour() {
153
+ return this.setMinutes(59, 59, 999);
154
+ };
155
+
156
+ /**
157
+ * Set the DateTime to the end of the minute.
158
+ * @return {DateTime} The DateTime object.
159
+ */
160
+ export function endOfMinute() {
161
+ return this.setSeconds(59, 999);
162
+ };
163
+
164
+ /**
165
+ * Set the DateTime to the end of the month.
166
+ * @return {DateTime} The DateTime object.
167
+ */
168
+ export function endOfMonth() {
169
+ return this.setDate(this.daysInMonth())
170
+ .endOfDay();
171
+ }
172
+
173
+ /**
174
+ * Set the DateTime to the end of the quarter.
175
+ * @return {DateTime} The DateTime object.
176
+ */
177
+ export function endOfQuarter() {
178
+ const month = this.getQuarter() * 3;
179
+ return this.setMonth(month, daysInMonth(this.getYear(), month))
180
+ .endOfDay();
181
+ };
182
+
183
+ /**
184
+ * Set the DateTime to the end of the second.
185
+ * @return {DateTime} The DateTime object.
186
+ */
187
+ export function endOfSecond() {
188
+ return this.setMilliseconds(999);
189
+ };
190
+
191
+ /**
192
+ * Set the DateTime to the end of the week.
193
+ * @return {DateTime} The DateTime object.
194
+ */
195
+ export function endOfWeek() {
196
+ return this.setWeekDay(7)
197
+ .endOfDay();
198
+ };
199
+
200
+ /**
201
+ * Set the DateTime to the end of the year.
202
+ * @return {DateTime} The DateTime object.
203
+ */
204
+ export function endOfYear() {
205
+ return this.setMonth(12, 31)
206
+ .endOfDay();
207
+ };
208
+
209
+ /**
210
+ * Set the DateTime to the start of the day.
211
+ * @return {DateTime} The DateTime object.
212
+ */
213
+ export function startOfDay() {
214
+ return this.setHours(0, 0, 0, 0);
215
+ };
216
+
217
+ /**
218
+ * Set the DateTime to the start of the hour.
219
+ * @return {DateTime} The DateTime object.
220
+ */
221
+ export function startOfHour() {
222
+ return this.setMinutes(0, 0, 0);
223
+ };
224
+
225
+ /**
226
+ * Set the DateTime to the start of the minute.
227
+ * @return {DateTime} The DateTime object.
228
+ */
229
+ export function startOfMinute() {
230
+ return this.setSeconds(0, 0);
231
+ };
232
+
233
+ /**
234
+ * Set the DateTime to the start of the month.
235
+ * @return {DateTime} The DateTime object.
236
+ */
237
+ export function startOfMonth() {
238
+ return this.setDate(1)
239
+ .startOfDay();
240
+ }
241
+
242
+ /**
243
+ * Set the DateTime to the start of the quarter.
244
+ * @return {DateTime} The DateTime object.
245
+ */
246
+ export function startOfQuarter() {
247
+ const month = this.getQuarter() * 3 - 2;
248
+ return this.setMonth(month, 1)
249
+ .startOfDay();
250
+ };
251
+
252
+ /**
253
+ * Set the DateTime to the start of the second.
254
+ * @return {DateTime} The DateTime object.
255
+ */
256
+ export function startOfSecond() {
257
+ return this.setMilliseconds(0);
258
+ };
259
+
260
+ /**
261
+ * Set the DateTime to the start of the week.
262
+ * @return {DateTime} The DateTime object.
263
+ */
264
+ export function startOfWeek() {
265
+ return this.setWeekDay(1)
266
+ .startOfDay();
267
+ };
268
+
269
+ /**
270
+ * Set the DateTime to the start of the year.
271
+ * @return {DateTime} The DateTime object.
272
+ */
273
+ export function startOfYear() {
274
+ return this.setMonth(1, 1)
275
+ .startOfDay();
276
+ };
277
+
278
+ /**
279
+ * Subtract a day from the current DateTime.
280
+ * @return {DateTime} The DateTime object.
281
+ */
282
+ export function subDay() {
283
+ return this.addDays(-1);
284
+ };
285
+
286
+ /**
287
+ * Subtract days from the current DateTime.
288
+ * @param {number} amount The number of days to subtract.
289
+ * @return {DateTime} The DateTime object.
290
+ */
291
+ export function subDays(amount) {
292
+ return this.addDays(-amount);
293
+ };
25
294
 
26
- switch (timeUnit) {
27
- case 'second':
28
- return this.setMilliseconds(999);
29
- case 'minute':
30
- return this.setSeconds(59, 999);
31
- case 'hour':
32
- return this.setMinutes(59, 59, 999);
33
- case 'day':
34
- return this.setHours(23, 59, 59, 999);
35
- case 'week':
36
- return this.setWeekDay(7)
37
- .setHours(23, 59, 59, 999);
38
- case 'month':
39
- return this.setDate(this.daysInMonth())
40
- .setHours(23, 59, 59, 999);
41
- case 'quarter':
42
- const month = this.getQuarter() * 3;
43
- return this.setMonth(month, daysInMonth(this.getYear(), month))
44
- .setHours(23, 59, 59, 999);
45
- case 'year':
46
- return this.setMonth(12, 31)
47
- .setHours(23, 59, 59, 999);
48
- default:
49
- throw new Error('Invalid time unit supplied');
50
- }
295
+ /**
296
+ * Subtract an hour from the current DateTime.
297
+ * @return {DateTime} The DateTime object.
298
+ */
299
+ export function subHour() {
300
+ return this.addHours(-1);
51
301
  };
52
302
 
53
303
  /**
54
- * Modify the DateTime by setting it to the start of a unit of time.
55
- * @param {string} timeUnit The unit of time.
304
+ * Subtract hours from the current DateTime.
305
+ * @param {number} amount The number of hours to subtract.
56
306
  * @return {DateTime} The DateTime object.
57
307
  */
58
- export function startOf(timeUnit) {
59
- timeUnit = timeUnit.toLowerCase();
308
+ export function subHours(amount) {
309
+ return this.addHours(-amount);
310
+ };
60
311
 
61
- switch (timeUnit) {
62
- case 'second':
63
- return this.setMilliseconds(0);
64
- case 'minute':
65
- return this.setSeconds(0, 0);
66
- case 'hour':
67
- return this.setMinutes(0, 0, 0);
68
- case 'day':
69
- return this.setHours(0, 0, 0, 0);
70
- case 'week':
71
- return this.setWeekDay(1)
72
- .setHours(0, 0, 0, 0);
73
- case 'month':
74
- return this.setDate(1)
75
- .setHours(0, 0, 0, 0);
76
- case 'quarter':
77
- const month = this.getQuarter() * 3 - 2;
78
- return this.setMonth(month, 1)
79
- .setHours(0, 0, 0, 0);
80
- case 'year':
81
- return this.setMonth(1, 1)
82
- .setHours(0, 0, 0, 0);
83
- default:
84
- throw new Error('Invalid time unit supplied');
85
- }
312
+ /**
313
+ * Subtract a minute from the current DateTime.
314
+ * @return {DateTime} The DateTime object.
315
+ */
316
+ export function subMinute() {
317
+ return this.addMinutes(-1);
318
+ };
319
+
320
+ /**
321
+ * Subtract minutes from the current DateTime.
322
+ * @param {number} amount The number of minutes to subtract.
323
+ * @return {DateTime} The DateTime object.
324
+ */
325
+ export function subMinutes(amount) {
326
+ return this.addMinutes(-amount);
327
+ };
328
+
329
+ /**
330
+ * Subtract a month from the current DateTime.
331
+ * @return {DateTime} The DateTime object.
332
+ */
333
+ export function subMonth() {
334
+ return this.addMonths(-1);
335
+ };
336
+
337
+ /**
338
+ * Subtract months from the current DateTime.
339
+ * @param {number} amount The number of months to subtract.
340
+ * @return {DateTime} The DateTime object.
341
+ */
342
+ export function subMonths(amount) {
343
+ return this.addMonths(-amount);
344
+ };
345
+
346
+ /**
347
+ * Subtract a second from the current DateTime.
348
+ * @return {DateTime} The DateTime object.
349
+ */
350
+ export function subSecond() {
351
+ return this.addSeconds(-1);
352
+ };
353
+
354
+ /**
355
+ * Subtract seconds from the current DateTime.
356
+ * @param {number} amount The number of seconds to subtract.
357
+ * @return {DateTime} The DateTime object.
358
+ */
359
+ export function subSeconds(amount) {
360
+ return this.addSeconds(-amount);
361
+ };
362
+
363
+ /**
364
+ * Subtract a week from the current DateTime.
365
+ * @return {DateTime} The DateTime object.
366
+ */
367
+ export function subWeek() {
368
+ return this.addWeeks(-1);
369
+ };
370
+
371
+ /**
372
+ * Subtract weeks from the current DateTime.
373
+ * @param {number} amount The number of weeks to subtract.
374
+ * @return {DateTime} The DateTime object.
375
+ */
376
+ export function subWeeks(amount) {
377
+ return this.addWeeks(-amount);
378
+ };
379
+
380
+ /**
381
+ * Subtract a year from the current DateTime.
382
+ * @return {DateTime} The DateTime object.
383
+ */
384
+ export function subYear() {
385
+ return this.addYears(-1);
86
386
  };
87
387
 
88
388
  /**
89
- * Subtract a duration from the date.
90
- * @param {number} amount The amount to modify the date by.
91
- * @param {string} timeUnit The unit of time.
389
+ * Subtract years from the current DateTime.
390
+ * @param {number} amount The number of years to subtract.
92
391
  * @return {DateTime} The DateTime object.
93
392
  */
94
- export function sub(amount, timeUnit) {
95
- return modify(this, -amount, timeUnit);
393
+ export function subYears(amount) {
394
+ return this.addYears(-amount);
96
395
  };
@@ -1,6 +1,4 @@
1
1
  import DateTime from './../date-time.js';
2
- import { getRelativeFormatter } from './../factory.js';
3
- import { compensateDiff, getBiggestDiff } from './../helpers.js';
4
2
  import { formatDay, formatDayPeriod, formatEra, formatMonth, formatOffset, formatTimeZoneName } from './../formatter/format.js';
5
3
  import { minimumDays } from './../formatter/utility.js';
6
4
  import { daysInMonth as _daysInMonth, daysInYear as _daysInYear, isLeapYear as _isLeapYear } from './../static/utility.js';
@@ -54,136 +52,6 @@ export function daysInYear() {
54
52
  );
55
53
  };
56
54
 
57
- /**
58
- * Get the difference between this and another Date.
59
- * @param {DateTime} [other] The date to compare to.
60
- * @param {object} [options] The options for comparing the dates.
61
- * @param {string} [options.timeUnit] The unit of time.
62
- * @param {Boolean} [options.relative=true] Whether to use the relative difference.
63
- * @return {number} The difference.
64
- */
65
- export function diff(other, { timeUnit, relative = true } = {}) {
66
- if (!other) {
67
- other = new this.constructor;
68
- }
69
-
70
- if (!timeUnit) {
71
- return this - other;
72
- }
73
-
74
- if (timeUnit) {
75
- timeUnit = timeUnit.toLowerCase();
76
- }
77
-
78
- other = other.setTimeZone(this.getTimeZone());
79
-
80
- switch (timeUnit) {
81
- case 'year':
82
- case 'years':
83
- const yearDiff = this.getYear() - other.getYear();
84
- return compensateDiff(
85
- this,
86
- other.setYear(
87
- this.getYear(),
88
- ),
89
- yearDiff,
90
- !relative,
91
- -1,
92
- );
93
- case 'month':
94
- case 'months':
95
- const monthDiff = (this.getYear() - other.getYear()) *
96
- 12 +
97
- this.getMonth() -
98
- other.getMonth();
99
- return compensateDiff(
100
- this,
101
- other.setYear(
102
- this.getYear(),
103
- this.getMonth(),
104
- ),
105
- monthDiff,
106
- !relative,
107
- -1,
108
- );
109
- case 'week':
110
- case 'weeks':
111
- const weekDiff = (this - other) / 604800000;
112
- return compensateDiff(
113
- this,
114
- other.setWeekYear(
115
- this.getWeekYear(),
116
- this.getWeek(),
117
- ),
118
- weekDiff,
119
- relative,
120
- );
121
- case 'day':
122
- case 'days':
123
- const dayDiff = (this - other) / 86400000;
124
- return compensateDiff(
125
- this,
126
- other.setYear(
127
- this.getYear(),
128
- this.getMonth(),
129
- this.getDate(),
130
- ),
131
- dayDiff,
132
- relative,
133
- );
134
- case 'hour':
135
- case 'hours':
136
- const hourDiff = (this - other) / 3600000;
137
- return compensateDiff(
138
- this,
139
- other.setYear(
140
- this.getYear(),
141
- this.getMonth(),
142
- this.getDate(),
143
- ).setHours(
144
- this.getHours(),
145
- ),
146
- hourDiff,
147
- relative,
148
- );
149
- case 'minute':
150
- case 'minutes':
151
- const minuteDiff = (this - other) / 60000;
152
- return compensateDiff(
153
- this,
154
- other.setYear(
155
- this.getYear(),
156
- this.getMonth(),
157
- this.getDate(),
158
- ).setHours(
159
- this.getHours(),
160
- this.getMinutes(),
161
- ),
162
- minuteDiff,
163
- relative,
164
- );
165
- case 'second':
166
- case 'seconds':
167
- const secondDiff = (this - other) / 1000;
168
- return compensateDiff(
169
- this,
170
- other.setYear(
171
- this.getYear(),
172
- this.getMonth(),
173
- this.getDate(),
174
- ).setHours(
175
- this.getHours(),
176
- this.getMinutes(),
177
- this.getSeconds(),
178
- ),
179
- secondDiff,
180
- relative,
181
- );
182
- default:
183
- throw new Error('Invalid time unit supplied');
184
- }
185
- };
186
-
187
55
  /**
188
56
  * Get the era in current timeZone.
189
57
  * @param {string} [type=long] The type of era to return.
@@ -199,68 +67,6 @@ export function era(type = 'long') {
199
67
  );
200
68
  };
201
69
 
202
- /**
203
- * Get the difference between this and another Date in human readable form.
204
- * @param {DateTime} [other] The date to compare to.
205
- * @param {object} [options] The options for comparing the dates.
206
- * @param {string} [options.timeUnit] The unit of time.
207
- * @return {string} The difference in human readable form.
208
- */
209
- export function humanDiff(other, { timeUnit } = {}) {
210
- const relativeFormatter = getRelativeFormatter(this.getLocale());
211
-
212
- if (!relativeFormatter) {
213
- throw new Error('RelativeTimeFormat not supported');
214
- }
215
-
216
- if (!other) {
217
- other = new this.constructor;
218
- }
219
-
220
- let amount;
221
- if (timeUnit) {
222
- amount = this.diff(other, { timeUnit });
223
- } else {
224
- [amount, timeUnit] = getBiggestDiff(this, other);
225
- }
226
-
227
- return relativeFormatter.format(amount, timeUnit);
228
- };
229
-
230
- /**
231
- * Determine whether this DateTime is after another date (optionally to a granularity).
232
- * @param {DateTime} [other] The date to compare to.
233
- * @param {object} [options] The options for comparing the dates.
234
- * @param {string} [options.granularity] The level of granularity to use for comparison.
235
- * @return {Boolean} TRUE if this DateTime is after the other date, otherwise FALSE.
236
- */
237
- export function isAfter(other, { granularity } = {}) {
238
- return this.diff(other, { timeUnit: granularity }) > 0;
239
- };
240
-
241
- /**
242
- * Determine whether this DateTime is before another date (optionally to a granularity).
243
- * @param {DateTime} [other] The date to compare to.
244
- * @param {object} [options] The options for comparing the dates.
245
- * @param {string} [options.granularity] The level of granularity to use for comparison.
246
- * @return {Boolean} TRUE if this DateTime is before the other date, otherwise FALSE.
247
- */
248
- export function isBefore(other, { granularity } = {}) {
249
- return this.diff(other, { timeUnit: granularity }) < 0;
250
- };
251
-
252
- /**
253
- * Determine whether this DateTime is between two other dates (optionally to a granularity).
254
- * @param {DateTime} [start] The first date to compare to.
255
- * @param {DateTime} [end] The second date to compare to.
256
- * @param {object} [options] The options for comparing the dates.
257
- * @param {string} [options.granularity] The level of granularity to use for comparison.
258
- * @return {Boolean} TRUE if this DateTime is between the other dates, otherwise FALSE.
259
- */
260
- export function isBetween(start, end, { granularity } = {}) {
261
- return this.diff(start, { timeUnit: granularity }) > 0 && this.diff(end, { timeUnit: granularity }) < 0;
262
- };
263
-
264
70
  /**
265
71
  * Return true if the DateTime is in daylight savings.
266
72
  * @return {Boolean} TRUE if the current time is in daylight savings, otherwise FALSE.
@@ -291,39 +97,6 @@ export function isLeapYear() {
291
97
  );
292
98
  };
293
99
 
294
- /**
295
- * Determine whether this DateTime is the same as another date (optionally to a granularity).
296
- * @param {DateTime} [other] The date to compare to.
297
- * @param {object} [options] The options for comparing the dates.
298
- * @param {string} [options.granularity] The level of granularity to use for comparison.
299
- * @return {Boolean} TRUE if this DateTime is the same as the other date, otherwise FALSE.
300
- */
301
- export function isSame(other, { granularity } = {}) {
302
- return this.diff(other, { timeUnit: granularity }) === 0;
303
- };
304
-
305
- /**
306
- * Determine whether this DateTime is the same or after another date (optionally to a granularity).
307
- * @param {DateTime} [other] The date to compare to.
308
- * @param {object} [options] The options for comparing the dates.
309
- * @param {string} [options.granularity] The level of granularity to use for comparison.
310
- * @return {Boolean} TRUE if this DateTime is the same or after the other date, otherwise FALSE.
311
- */
312
- export function isSameOrAfter(other, { granularity } = {}) {
313
- return this.diff(other, { timeUnit: granularity }) >= 0;
314
- };
315
-
316
- /**
317
- * Determine whether this DateTime is the same or before another date.
318
- * @param {DateTime} other The date to compare to.
319
- * @param {object} [options] The options for comparing the dates.
320
- * @param {string} [options.granularity] The level of granularity to use for comparison.
321
- * @return {Boolean} TRUE if this DateTime is the same or before the other date, otherwise FALSE.
322
- */
323
- export function isSameOrBefore(other, { granularity } = {}) {
324
- return this.diff(other, { timeUnit: granularity }) <= 0;
325
- };
326
-
327
100
  /**
328
101
  * Get the name of the month in current timeZone.
329
102
  * @param {string} [type=long] The type of month name to return.