@fr0st/datetime 5.1.5 → 6.0.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.
@@ -113,6 +113,16 @@
113
113
  ['minutes', 'seconds', 'milliseconds'],
114
114
  ];
115
115
 
116
+ const diffMethods = {
117
+ year: 'diffInYears',
118
+ month: 'diffInMonths',
119
+ week: 'diffInWeeks',
120
+ day: 'diffInDays',
121
+ hour: 'diffInHours',
122
+ minute: 'diffInMinutes',
123
+ second: 'diffInSeconds',
124
+ };
125
+
116
126
  const thresholds = {
117
127
  month: 12,
118
128
  week: null,
@@ -126,6 +136,98 @@
126
136
  * DateTime Helpers
127
137
  */
128
138
 
139
+ function calculateDiff(date, other, timeUnit, relative = true) {
140
+ other = other.setTimeZone(date.getTimeZone());
141
+
142
+ switch (timeUnit) {
143
+ case 'year':
144
+ return compensateDiff(
145
+ date,
146
+ other.setYear(
147
+ date.getYear(),
148
+ ),
149
+ date.getYear() - other.getYear(),
150
+ !relative,
151
+ -1,
152
+ );
153
+ case 'month':
154
+ return compensateDiff(
155
+ date,
156
+ other.setYear(
157
+ date.getYear(),
158
+ date.getMonth(),
159
+ ),
160
+ (date.getYear() - other.getYear()) * 12 + date.getMonth() - other.getMonth(),
161
+ !relative,
162
+ -1,
163
+ );
164
+ case 'week':
165
+ return compensateDiff(
166
+ date,
167
+ other.setWeekYear(
168
+ date.getWeekYear(),
169
+ date.getWeek(),
170
+ ),
171
+ (date - other) / 604800000,
172
+ relative,
173
+ );
174
+ case 'day':
175
+ return compensateDiff(
176
+ date,
177
+ other.setYear(
178
+ date.getYear(),
179
+ date.getMonth(),
180
+ date.getDate(),
181
+ ),
182
+ (date - other) / 86400000,
183
+ relative,
184
+ );
185
+ case 'hour':
186
+ return compensateDiff(
187
+ date,
188
+ other.setYear(
189
+ date.getYear(),
190
+ date.getMonth(),
191
+ date.getDate(),
192
+ ).setHours(
193
+ date.getHours(),
194
+ ),
195
+ (date - other) / 3600000,
196
+ relative,
197
+ );
198
+ case 'minute':
199
+ return compensateDiff(
200
+ date,
201
+ other.setYear(
202
+ date.getYear(),
203
+ date.getMonth(),
204
+ date.getDate(),
205
+ ).setHours(
206
+ date.getHours(),
207
+ date.getMinutes(),
208
+ ),
209
+ (date - other) / 60000,
210
+ relative,
211
+ );
212
+ case 'second':
213
+ return compensateDiff(
214
+ date,
215
+ other.setYear(
216
+ date.getYear(),
217
+ date.getMonth(),
218
+ date.getDate(),
219
+ ).setHours(
220
+ date.getHours(),
221
+ date.getMinutes(),
222
+ date.getSeconds(),
223
+ ),
224
+ (date - other) / 1000,
225
+ relative,
226
+ );
227
+ default:
228
+ throw new Error('Invalid time unit supplied');
229
+ }
230
+ }
129
231
  /**
130
232
  * Compensate the difference between two dates.
131
233
  * @param {DateTime} date The DateTime.
@@ -160,13 +262,15 @@
160
262
  */
161
263
  function getBiggestDiff(date, other) {
162
264
  let lastResult;
163
- for (const timeUnit of ['year', 'month', 'week', 'day', 'hour', 'minute', 'second']) {
164
- const relativeDiff = date.diff(other, { timeUnit });
265
+ for (const [timeUnit, diffMethod] of Object.entries(diffMethods)) {
266
+ const relativeDiff = date[diffMethod](other);
267
+
165
268
  if (lastResult && thresholds[timeUnit] && Math.abs(relativeDiff) >= thresholds[timeUnit]) {
166
269
  return lastResult;
167
270
  }
168
271
 
169
- const actualDiff = date.diff(other, { timeUnit, relative: false });
272
+ const actualDiff = date[diffMethod](other, { relative: false });
273
+
170
274
  if (actualDiff) {
171
275
  return [relativeDiff, timeUnit];
172
276
  }
@@ -207,56 +311,6 @@
207
311
  function getOffsetTime(date) {
208
312
  return date.getTime() - (date.getTimeZoneOffset() * 60000);
209
313
  }
210
- /**
211
- * Modify a DateTime by a duration.
212
- * @param {DateTime} date The DateTime.
213
- * @param {number} amount The amount to modify the date by.
214
- * @param {string} [timeUnit] The unit of time.
215
- * @return {DateTime} The DateTime object.
216
- */
217
- function modify(date, amount, timeUnit) {
218
- timeUnit = timeUnit.toLowerCase();
219
-
220
- switch (timeUnit) {
221
- case 'second':
222
- case 'seconds':
223
- return date.setSeconds(
224
- date.getSeconds() + amount,
225
- );
226
- case 'minute':
227
- case 'minutes':
228
- return date.setMinutes(
229
- date.getMinutes() + amount,
230
- );
231
- case 'hour':
232
- case 'hours':
233
- return date.setHours(
234
- date.getHours() + amount,
235
- );
236
- case 'week':
237
- case 'weeks':
238
- return date.setDate(
239
- date.getDate() + (amount * 7),
240
- );
241
- case 'day':
242
- case 'days':
243
- return date.setDate(
244
- date.getDate() + amount,
245
- );
246
- case 'month':
247
- case 'months':
248
- return date.setMonth(
249
- date.getMonth() + amount,
250
- );
251
- case 'year':
252
- case 'years':
253
- return date.setYear(
254
- date.getYear() + amount,
255
- );
256
- default:
257
- throw new Error('Invalid time unit supplied');
258
- }
259
- }
260
314
  /**
261
315
  * Compare a literal format string with a date string.
262
316
  * @param {string} formatString The literal format string.
@@ -382,7 +436,19 @@
382
436
  * @return {DateTime} The DateTime object.
383
437
  */
384
438
  function setOffsetTime(date, time) {
385
- return date.setTime(time + (date.getTimeZoneOffset() * 60000));
439
+ const oldOffset = date.getTimeZoneOffset();
440
+
441
+ const newTime = time + (oldOffset * 60000);
442
+ const newDate = date.setTime(newTime);
443
+
444
+ const offset = newDate.getTimeZoneOffset();
445
+
446
+ if (oldOffset === offset) {
447
+ return newDate;
448
+ }
449
+
450
+ // compensate for DST transitions
451
+ return newDate.setTime(newTime - ((oldOffset - offset) * 60000));
386
452
  }
387
453
 
388
454
  /**
@@ -602,6 +668,22 @@
602
668
 
603
669
  return `${sign}${hourString}${colon}${minuteString}`;
604
670
  }
671
+ /**
672
+ * Format a relative duration as a locale string.
673
+ * @param {string} locale The locale.
674
+ * @param {number} amount The amount of duration.
675
+ * @param {string} unit The time unit.
676
+ * @returns {string} The relative duration.
677
+ */
678
+ function formatRelative(locale, amount, unit) {
679
+ const relativeFormatter = getRelativeFormatter(locale);
680
+
681
+ if (!relativeFormatter) {
682
+ throw new Error('RelativeTimeFormat not supported');
683
+ }
684
+
685
+ return relativeFormatter.format(amount, unit);
686
+ }
605
687
  /**
606
688
  * Format a time zone as a locale string.
607
689
  * @param {string} locale The locale.
@@ -2009,7 +2091,7 @@
2009
2091
  * @return {number} The local week. (1, 53)
2010
2092
  */
2011
2093
  function getWeek() {
2012
- const thisWeek = this.startOf('day').setWeekDay(1);
2094
+ const thisWeek = this.startOfDay().setWeekDay(1);
2013
2095
  const firstWeek = thisWeek.setWeek(1, 1);
2014
2096
 
2015
2097
  return 1 +
@@ -2333,423 +2415,1085 @@
2333
2415
  }
2334
2416
 
2335
2417
  /**
2336
- * DateTime Manipulation
2418
+ * DateTime Comparisons
2337
2419
  */
2338
2420
 
2339
2421
  /**
2340
- * Add a duration to the date.
2341
- * @param {number} amount The amount to modify the date by.
2342
- * @param {string} timeUnit The unit of time.
2343
- * @return {DateTime} The DateTime object.
2422
+ * Get the difference between this and another Date in milliseconds.
2423
+ * @param {DateTime} other The date to compare to.
2424
+ * @return {number} The difference.
2344
2425
  */
2345
- function add(amount, timeUnit) {
2346
- return modify(this, amount, timeUnit);
2426
+ function diff(other) {
2427
+ return this - other;
2347
2428
  }
2348
2429
  /**
2349
- * Modify the DateTime by setting it to the end of a unit of time.
2350
- * @param {string} timeUnit The unit of time.
2351
- * @return {DateTime} The DateTime object.
2430
+ * Get the difference between this and another Date in days.
2431
+ * @param {DateTime} other The date to compare to.
2432
+ * @param {object} [options] The options for comparing the dates.
2433
+ * @param {Boolean} [options.relative=true] Whether to use the relative difference.
2434
+ * @return {number} The difference.
2352
2435
  */
2353
- function endOf(timeUnit) {
2354
- timeUnit = timeUnit.toLowerCase();
2355
-
2356
- switch (timeUnit) {
2357
- case 'second':
2358
- return this.setMilliseconds(999);
2359
- case 'minute':
2360
- return this.setSeconds(59, 999);
2361
- case 'hour':
2362
- return this.setMinutes(59, 59, 999);
2363
- case 'day':
2364
- return this.setHours(23, 59, 59, 999);
2365
- case 'week':
2366
- return this.setWeekDay(7)
2367
- .setHours(23, 59, 59, 999);
2368
- case 'month':
2369
- return this.setDate(this.daysInMonth())
2370
- .setHours(23, 59, 59, 999);
2371
- case 'quarter':
2372
- const month = this.getQuarter() * 3;
2373
- return this.setMonth(month, daysInMonth$1(this.getYear(), month))
2374
- .setHours(23, 59, 59, 999);
2375
- case 'year':
2376
- return this.setMonth(12, 31)
2377
- .setHours(23, 59, 59, 999);
2378
- default:
2379
- throw new Error('Invalid time unit supplied');
2380
- }
2436
+ function diffInDays(other, { relative = true } = {}) {
2437
+ return calculateDiff(this, other, 'day', relative);
2381
2438
  }
2382
2439
  /**
2383
- * Modify the DateTime by setting it to the start of a unit of time.
2384
- * @param {string} timeUnit The unit of time.
2385
- * @return {DateTime} The DateTime object.
2440
+ * Get the difference between this and another Date in hours.
2441
+ * @param {DateTime} other The date to compare to.
2442
+ * @param {object} [options] The options for comparing the dates.
2443
+ * @param {Boolean} [options.relative=true] Whether to use the relative difference.
2444
+ * @return {number} The difference.
2386
2445
  */
2387
- function startOf(timeUnit) {
2388
- timeUnit = timeUnit.toLowerCase();
2389
-
2390
- switch (timeUnit) {
2391
- case 'second':
2392
- return this.setMilliseconds(0);
2393
- case 'minute':
2394
- return this.setSeconds(0, 0);
2395
- case 'hour':
2396
- return this.setMinutes(0, 0, 0);
2397
- case 'day':
2398
- return this.setHours(0, 0, 0, 0);
2399
- case 'week':
2400
- return this.setWeekDay(1)
2401
- .setHours(0, 0, 0, 0);
2402
- case 'month':
2403
- return this.setDate(1)
2404
- .setHours(0, 0, 0, 0);
2405
- case 'quarter':
2406
- const month = this.getQuarter() * 3 - 2;
2407
- return this.setMonth(month, 1)
2408
- .setHours(0, 0, 0, 0);
2409
- case 'year':
2410
- return this.setMonth(1, 1)
2411
- .setHours(0, 0, 0, 0);
2412
- default:
2413
- throw new Error('Invalid time unit supplied');
2414
- }
2446
+ function diffInHours(other, { relative = true } = {}) {
2447
+ return calculateDiff(this, other, 'hour', relative);
2415
2448
  }
2416
2449
  /**
2417
- * Subtract a duration from the date.
2418
- * @param {number} amount The amount to modify the date by.
2419
- * @param {string} timeUnit The unit of time.
2420
- * @return {DateTime} The DateTime object.
2450
+ * Get the difference between this and another Date in minutes.
2451
+ * @param {DateTime} other The date to compare to.
2452
+ * @param {object} [options] The options for comparing the dates.
2453
+ * @param {Boolean} [options.relative=true] Whether to use the relative difference.
2454
+ * @return {number} The difference.
2421
2455
  */
2422
- function sub(amount, timeUnit) {
2423
- return modify(this, -amount, timeUnit);
2456
+ function diffInMinutes(other, { relative = true } = {}) {
2457
+ return calculateDiff(this, other, 'minute', relative);
2424
2458
  }
2425
-
2426
2459
  /**
2427
- * DateTime Output
2460
+ * Get the difference between this and another Date in months.
2461
+ * @param {DateTime} other The date to compare to.
2462
+ * @param {object} [options] The options for comparing the dates.
2463
+ * @param {Boolean} [options.relative=true] Whether to use the relative difference.
2464
+ * @return {number} The difference.
2428
2465
  */
2429
-
2466
+ function diffInMonths(other, { relative = true } = {}) {
2467
+ return calculateDiff(this, other, 'month', relative);
2468
+ }
2430
2469
  /**
2431
- * Format the current date using a format string.
2432
- * @param {string} formatString The format string.
2433
- * @return {string} The formatted date string.
2470
+ * Get the difference between this and another Date in seconds.
2471
+ * @param {DateTime} other The date to compare to.
2472
+ * @param {object} [options] The options for comparing the dates.
2473
+ * @param {Boolean} [options.relative=true] Whether to use the relative difference.
2474
+ * @return {number} The difference.
2434
2475
  */
2435
- function format(formatString) {
2436
- let match;
2437
- let output = '';
2438
-
2439
- while (formatString && (match = formatString.match(formatTokenRegExp))) {
2440
- const token = match[1];
2441
- const position = match.index;
2442
- const length = match[0].length;
2443
-
2444
- if (position) {
2445
- output += formatString.substring(0, position);
2446
- }
2447
-
2448
- formatString = formatString.substring(position + length);
2449
-
2450
- if (!token) {
2451
- output += match[0].slice(1, -1);
2452
- continue;
2453
- }
2454
-
2455
- if (!(token in tokens)) {
2456
- throw new Error(`Invalid token in DateTime format: ${token}`);
2457
- }
2458
-
2459
- output += tokens[token].output(this, length);
2460
- }
2461
-
2462
- output += formatString;
2463
-
2464
- return output;
2476
+ function diffInSeconds(other, { relative = true } = {}) {
2477
+ return calculateDiff(this, other, 'second', relative);
2465
2478
  }
2466
2479
  /**
2467
- * Format the current date using "eee MMM dd yyyy".
2468
- * @return {string} The formatted date string.
2480
+ * Get the difference between this and another Date in weeks.
2481
+ * @param {DateTime} other The date to compare to.
2482
+ * @param {object} [options] The options for comparing the dates.
2483
+ * @param {Boolean} [options.relative=true] Whether to use the relative difference.
2484
+ * @return {number} The difference.
2469
2485
  */
2470
- function toDateString() {
2471
- return this.format(formats.date);
2486
+ function diffInWeeks(other, { relative = true } = {}) {
2487
+ return calculateDiff(this, other, 'week', relative);
2472
2488
  }
2473
2489
  /**
2474
- * Format the current date using "yyyy-MM-dd'THH:mm:ss.SSSSSSxxx".
2475
- * @return {string} The formatted date string.
2490
+ * Get the difference between this and another Date in years.
2491
+ * @param {DateTime} other The date to compare to.
2492
+ * @param {object} [options] The options for comparing the dates.
2493
+ * @param {Boolean} [options.relative=true] Whether to use the relative difference.
2494
+ * @return {number} The difference.
2476
2495
  */
2477
- function toISOString() {
2478
- return this
2479
- .setLocale('en')
2480
- .setTimeZone('UTC')
2481
- .format(formats.rfc3339_extended);
2496
+ function diffInYears(other, { relative = true } = {}) {
2497
+ return calculateDiff(this, other, 'year', relative);
2482
2498
  }
2483
2499
  /**
2484
- * Format the current date using "eee MMM dd yyyy HH:mm:ss xx (VV)".
2485
- * @return {string} The formatted date string.
2500
+ * Get the difference between this and another Date in human readable form.
2501
+ * @param {DateTime} other The date to compare to.
2502
+ * @return {string} The difference in human readable form.
2486
2503
  */
2487
- function toString() {
2488
- return this.format(formats.string);
2504
+ function humanDiff(other) {
2505
+ const [amount, unit] = getBiggestDiff(this, other);
2506
+ return formatRelative(this.getLocale(), amount, unit);
2489
2507
  }
2490
2508
  /**
2491
- * Format the current date using "HH:mm:ss xx (VV)".
2492
- * @return {string} The formatted date string.
2509
+ * Get the difference between this and another Date in days in human readable form.
2510
+ * @param {DateTime} other The date to compare to.
2511
+ * @return {string} The difference in days in human readable form.
2493
2512
  */
2494
- function toTimeString() {
2495
- return this.format(formats.time);
2513
+ function humanDiffInDays(other) {
2514
+ return formatRelative(this.getLocale(), this.diffInDays(other), 'day');
2496
2515
  }
2497
2516
  /**
2498
- * Format the current date in UTC timeZone using "eee MMM dd yyyy HH:mm:ss xx (VV)".
2499
- * @return {string} The formatted date string.
2517
+ * Get the difference between this and another Date in hours in human readable form.
2518
+ * @param {DateTime} other The date to compare to.
2519
+ * @return {string} The difference in hours in human readable form.
2500
2520
  */
2501
- function toUTCString() {
2502
- return this
2503
- .setLocale('en')
2504
- .setTimeZone('UTC')
2505
- .toString();
2521
+ function humanDiffInHours(other) {
2522
+ return formatRelative(this.getLocale(), this.diffInHours(other), 'hour');
2506
2523
  }
2507
-
2508
2524
  /**
2509
- * DateTime Utility
2525
+ * Get the difference between this and another Date in minutes in human readable form.
2526
+ * @param {DateTime} other The date to compare to.
2527
+ * @return {string} The difference in minutes in human readable form.
2510
2528
  */
2511
-
2529
+ function humanDiffInMinutes(other) {
2530
+ return formatRelative(this.getLocale(), this.diffInMinutes(other), 'minute');
2531
+ }
2512
2532
  /**
2513
- * Get the name of the day of the week in current timeZone.
2514
- * @param {string} [type=long] The type of day name to return.
2515
- * @return {string} The name of the day of the week.
2533
+ * Get the difference between this and another Date in months in human readable form.
2534
+ * @param {DateTime} other The date to compare to.
2535
+ * @return {string} The difference in months in human readable form.
2516
2536
  */
2517
- function dayName(type = 'long') {
2518
- return formatDay(this.getLocale(), this.getDay(), type);
2537
+ function humanDiffInMonths(other) {
2538
+ return formatRelative(this.getLocale(), this.diffInMonths(other), 'month');
2519
2539
  }
2520
2540
  /**
2521
- * Get the day period in current timeZone.
2522
- * @param {string} [type=long] The type of day period to return.
2523
- * @return {string} The day period.
2541
+ * Get the difference between this and another Date in seconds in human readable form.
2542
+ * @param {DateTime} other The date to compare to.
2543
+ * @return {string} The difference in seconds in human readable form.
2524
2544
  */
2525
- function dayPeriod(type = 'long') {
2526
- return formatDayPeriod(
2527
- this.getLocale(),
2528
- this.getHours() < 12 ?
2529
- 0 :
2530
- 1,
2531
- type,
2532
- );
2545
+ function humanDiffInSeconds(other) {
2546
+ return formatRelative(this.getLocale(), this.diffInSeconds(other), 'second');
2533
2547
  }
2534
2548
  /**
2535
- * Get the number of days in the current month.
2536
- * @return {number} The number of days in the current month.
2549
+ * Get the difference between this and another Date in weeks in human readable form.
2550
+ * @param {DateTime} other The date to compare to.
2551
+ * @return {string} The difference in weeks in human readable form.
2537
2552
  */
2538
- function daysInMonth() {
2539
- return daysInMonth$1(
2540
- this.getYear(),
2541
- this.getMonth(),
2553
+ function humanDiffInWeeks(other) {
2554
+ return formatRelative(this.getLocale(), this.diffInWeeks(other), 'week');
2555
+ }
2556
+ /**
2557
+ * Get the difference between this and another Date in years in human readable form.
2558
+ * @param {DateTime} other The date to compare to.
2559
+ * @return {string} The difference in years in human readable form.
2560
+ */
2561
+ function humanDiffInYears(other) {
2562
+ return formatRelative(this.getLocale(), this.diffInYears(other), 'year');
2563
+ }
2564
+ /**
2565
+ * Determine whether this DateTime is after another date.
2566
+ * @param {DateTime} other The date to compare to.
2567
+ * @return {Boolean} TRUE if this DateTime is after the other date, otherwise FALSE.
2568
+ */
2569
+ function isAfter(other) {
2570
+ return this.diff(other) > 0;
2571
+ }
2572
+
2573
+ /**
2574
+ * Determine whether this DateTime is after another date (comparing by day).
2575
+ * @param {DateTime} other The date to compare to.
2576
+ * @return {Boolean} TRUE if this DateTime is after the other date (comparing by day), otherwise FALSE.
2577
+ */
2578
+ function isAfterDay(other) {
2579
+ return this.diffInDays(other) > 0;
2580
+ }
2581
+
2582
+ /**
2583
+ * Determine whether this DateTime is after another date (comparing by hour).
2584
+ * @param {DateTime} other The date to compare to.
2585
+ * @return {Boolean} TRUE if this DateTime is after the other date (comparing by hour), otherwise FALSE.
2586
+ */
2587
+ function isAfterHour(other) {
2588
+ return this.diffInHours(other) > 0;
2589
+ }
2590
+
2591
+ /**
2592
+ * Determine whether this DateTime is after another date (comparing by minute).
2593
+ * @param {DateTime} other The date to compare to.
2594
+ * @return {Boolean} TRUE if this DateTime is after the other date (comparing by minute), otherwise FALSE.
2595
+ */
2596
+ function isAfterMinute(other) {
2597
+ return this.diffInMinutes(other) > 0;
2598
+ }
2599
+
2600
+ /**
2601
+ * Determine whether this DateTime is after another date (comparing by month).
2602
+ * @param {DateTime} other The date to compare to.
2603
+ * @return {Boolean} TRUE if this DateTime is after the other date (comparing by month), otherwise FALSE.
2604
+ */
2605
+ function isAfterMonth(other) {
2606
+ return this.diffInMonths(other) > 0;
2607
+ }
2608
+
2609
+ /**
2610
+ * Determine whether this DateTime is after another date (comparing by second).
2611
+ * @param {DateTime} other The date to compare to.
2612
+ * @return {Boolean} TRUE if this DateTime is after the other date (comparing by second), otherwise FALSE.
2613
+ */
2614
+ function isAfterSecond(other) {
2615
+ return this.diffInSeconds(other) > 0;
2616
+ }
2617
+
2618
+ /**
2619
+ * Determine whether this DateTime is after another date (comparing by week).
2620
+ * @param {DateTime} other The date to compare to.
2621
+ * @return {Boolean} TRUE if this DateTime is after the other date (comparing by week), otherwise FALSE.
2622
+ */
2623
+ function isAfterWeek(other) {
2624
+ return this.diffInWeeks(other) > 0;
2625
+ }
2626
+
2627
+ /**
2628
+ * Determine whether this DateTime is after another date (comparing by year).
2629
+ * @param {DateTime} other The date to compare to.
2630
+ * @return {Boolean} TRUE if this DateTime is after the other date (comparing by year), otherwise FALSE.
2631
+ */
2632
+ function isAfterYear(other) {
2633
+ return this.diffInYears(other) > 0;
2634
+ }
2635
+
2636
+ /**
2637
+ * Determine whether this DateTime is before another date.
2638
+ * @param {DateTime} other The date to compare to.
2639
+ * @return {Boolean} TRUE if this DateTime is before the other date, otherwise FALSE.
2640
+ */
2641
+ function isBefore(other) {
2642
+ return this.diff(other) < 0;
2643
+ }
2644
+
2645
+ /**
2646
+ * Determine whether this DateTime is before another date (comparing by day).
2647
+ * @param {DateTime} other The date to compare to.
2648
+ * @return {Boolean} TRUE if this DateTime is before the other date (comparing by day), otherwise FALSE.
2649
+ */
2650
+ function isBeforeDay(other) {
2651
+ return this.diffInDays(other) < 0;
2652
+ }
2653
+
2654
+ /**
2655
+ * Determine whether this DateTime is before another date (comparing by hour).
2656
+ * @param {DateTime} other The date to compare to.
2657
+ * @return {Boolean} TRUE if this DateTime is before the other date (comparing by hour), otherwise FALSE.
2658
+ */
2659
+ function isBeforeHour(other) {
2660
+ return this.diffInHours(other) < 0;
2661
+ }
2662
+
2663
+ /**
2664
+ * Determine whether this DateTime is before another date (comparing by minute).
2665
+ * @param {DateTime} other The date to compare to.
2666
+ * @return {Boolean} TRUE if this DateTime is before the other date (comparing by minute), otherwise FALSE.
2667
+ */
2668
+ function isBeforeMinute(other) {
2669
+ return this.diffInMinutes(other) < 0;
2670
+ }
2671
+
2672
+ /**
2673
+ * Determine whether this DateTime is before another date (comparing by month).
2674
+ * @param {DateTime} other The date to compare to.
2675
+ * @return {Boolean} TRUE if this DateTime is before the other date (comparing by month), otherwise FALSE.
2676
+ */
2677
+ function isBeforeMonth(other) {
2678
+ return this.diffInMonths(other) < 0;
2679
+ }
2680
+
2681
+ /**
2682
+ * Determine whether this DateTime is before another date (comparing by second).
2683
+ * @param {DateTime} other The date to compare to.
2684
+ * @return {Boolean} TRUE if this DateTime is before the other date (comparing by second), otherwise FALSE.
2685
+ */
2686
+ function isBeforeSecond(other) {
2687
+ return this.diffInSeconds(other) < 0;
2688
+ }
2689
+
2690
+ /**
2691
+ * Determine whether this DateTime is before another date (comparing by week).
2692
+ * @param {DateTime} other The date to compare to.
2693
+ * @return {Boolean} TRUE if this DateTime is before the other date (comparing by week), otherwise FALSE.
2694
+ */
2695
+ function isBeforeWeek(other) {
2696
+ return this.diffInWeeks(other) < 0;
2697
+ }
2698
+
2699
+ /**
2700
+ * Determine whether this DateTime is before another date (comparing by year).
2701
+ * @param {DateTime} other The date to compare to.
2702
+ * @return {Boolean} TRUE if this DateTime is before the other date (comparing by year), otherwise FALSE.
2703
+ */
2704
+ function isBeforeYear(other) {
2705
+ return this.diffInYears(other) < 0;
2706
+ }
2707
+
2708
+ /**
2709
+ * Determine whether this DateTime is between two other dates.
2710
+ * @param {DateTime} start The first date to compare to.
2711
+ * @param {DateTime} end The second date to compare to.
2712
+ * @return {Boolean} TRUE if this DateTime is between two other dates, otherwise FALSE.
2713
+ */
2714
+ function isBetween(start, end) {
2715
+ return this.isAfter(start) && this.isBefore(end);
2716
+ }
2717
+
2718
+ /**
2719
+ * Determine whether this DateTime is between two other dates (comparing by day).
2720
+ * @param {DateTime} start The first date to compare to.
2721
+ * @param {DateTime} end The second date to compare to.
2722
+ * @return {Boolean} TRUE if this DateTime is between two other dates (comparing by day), otherwise FALSE.
2723
+ */
2724
+ function isBetweenDay(start, end) {
2725
+ return this.isAfterDay(start) && this.isBeforeDay(end);
2726
+ }
2727
+
2728
+ /**
2729
+ * Determine whether this DateTime is between two other dates (comparing by hour).
2730
+ * @param {DateTime} start The first date to compare to.
2731
+ * @param {DateTime} end The second date to compare to.
2732
+ * @return {Boolean} TRUE if this DateTime is between two other dates (comparing by hour), otherwise FALSE.
2733
+ */
2734
+ function isBetweenHour(start, end) {
2735
+ return this.isAfterHour(start) && this.isBeforeHour(end);
2736
+ }
2737
+
2738
+ /**
2739
+ * Determine whether this DateTime is between two other dates (comparing by minute).
2740
+ * @param {DateTime} start The first date to compare to.
2741
+ * @param {DateTime} end The second date to compare to.
2742
+ * @return {Boolean} TRUE if this DateTime is between two other dates (comparing by minute), otherwise FALSE.
2743
+ */
2744
+ function isBetweenMinute(start, end) {
2745
+ return this.isAfterMinute(start) && this.isBeforeMinute(end);
2746
+ }
2747
+
2748
+ /**
2749
+ * Determine whether this DateTime is between two other dates (comparing by month).
2750
+ * @param {DateTime} start The first date to compare to.
2751
+ * @param {DateTime} end The second date to compare to.
2752
+ * @return {Boolean} TRUE if this DateTime is between two other dates (comparing by month), otherwise FALSE.
2753
+ */
2754
+ function isBetweenMonth(start, end) {
2755
+ return this.isAfterMonth(start) && this.isBeforeMonth(end);
2756
+ }
2757
+
2758
+ /**
2759
+ * Determine whether this DateTime is between two other dates (comparing by second).
2760
+ * @param {DateTime} start The first date to compare to.
2761
+ * @param {DateTime} end The second date to compare to.
2762
+ * @return {Boolean} TRUE if this DateTime is between two other dates (comparing by second), otherwise FALSE.
2763
+ */
2764
+ function isBetweenSecond(start, end) {
2765
+ return this.isAfterSecond(start) && this.isBeforeSecond(end);
2766
+ }
2767
+
2768
+ /**
2769
+ * Determine whether this DateTime is between two other dates (comparing by week).
2770
+ * @param {DateTime} start The first date to compare to.
2771
+ * @param {DateTime} end The second date to compare to.
2772
+ * @return {Boolean} TRUE if this DateTime is between two other dates (comparing by week), otherwise FALSE.
2773
+ */
2774
+ function isBetweenWeek(start, end) {
2775
+ return this.isAfterWeek(start) && this.isBeforeWeek(end);
2776
+ }
2777
+
2778
+ /**
2779
+ * Determine whether this DateTime is between two other dates (comparing by year).
2780
+ * @param {DateTime} start The first date to compare to.
2781
+ * @param {DateTime} end The second date to compare to.
2782
+ * @return {Boolean} TRUE if this DateTime is between two other dates (comparing by year), otherwise FALSE.
2783
+ */
2784
+ function isBetweenYear(start, end) {
2785
+ return this.isAfterYear(start) && this.isBeforeYear(end);
2786
+ }
2787
+
2788
+ /**
2789
+ * Determine whether this DateTime is the same as another date.
2790
+ * @param {DateTime} other The date to compare to.
2791
+ * @return {Boolean} TRUE if this DateTime is the same as the other date, otherwise FALSE.
2792
+ */
2793
+ function isSame(other) {
2794
+ return this.diff(other) === 0;
2795
+ }
2796
+
2797
+ /**
2798
+ * Determine whether this DateTime is the same as another date (comparing by day).
2799
+ * @param {DateTime} other The date to compare to.
2800
+ * @return {Boolean} TRUE if this DateTime is the same as the other date (comparing by day), otherwise FALSE.
2801
+ */
2802
+ function isSameDay(other) {
2803
+ return this.diffInDays(other) === 0;
2804
+ }
2805
+
2806
+ /**
2807
+ * Determine whether this DateTime is the same as another date (comparing by hour).
2808
+ * @param {DateTime} other The date to compare to.
2809
+ * @return {Boolean} TRUE if this DateTime is the same as the other date (comparing by hour), otherwise FALSE.
2810
+ */
2811
+ function isSameHour(other) {
2812
+ return this.diffInHours(other) === 0;
2813
+ }
2814
+
2815
+ /**
2816
+ * Determine whether this DateTime is the same as another date (comparing by minute).
2817
+ * @param {DateTime} other The date to compare to.
2818
+ * @return {Boolean} TRUE if this DateTime is the same as the other date (comparing by minute), otherwise FALSE.
2819
+ */
2820
+ function isSameMinute(other) {
2821
+ return this.diffInMinutes(other) === 0;
2822
+ }
2823
+
2824
+ /**
2825
+ * Determine whether this DateTime is the same as another date (comparing by month).
2826
+ * @param {DateTime} other The date to compare to.
2827
+ * @return {Boolean} TRUE if this DateTime is the same as the other date (comparing by month), otherwise FALSE.
2828
+ */
2829
+ function isSameMonth(other) {
2830
+ return this.diffInMonths(other) === 0;
2831
+ }
2832
+
2833
+ /**
2834
+ * Determine whether this DateTime is the same as another date (comparing by second).
2835
+ * @param {DateTime} other The date to compare to.
2836
+ * @return {Boolean} TRUE if this DateTime is the same as the other date (comparing by second), otherwise FALSE.
2837
+ */
2838
+ function isSameSecond(other) {
2839
+ return this.diffInSeconds(other) === 0;
2840
+ }
2841
+
2842
+ /**
2843
+ * Determine whether this DateTime is the same as another date (comparing by week).
2844
+ * @param {DateTime} other The date to compare to.
2845
+ * @return {Boolean} TRUE if this DateTime is the same as the other date (comparing by week), otherwise FALSE.
2846
+ */
2847
+ function isSameWeek(other) {
2848
+ return this.diffInWeeks(other) === 0;
2849
+ }
2850
+
2851
+ /**
2852
+ * Determine whether this DateTime is the same as another date (comparing by year).
2853
+ * @param {DateTime} other The date to compare to.
2854
+ * @return {Boolean} TRUE if this DateTime is the same as the other date (comparing by year), otherwise FALSE.
2855
+ */
2856
+ function isSameYear(other) {
2857
+ return this.diffInYears(other) === 0;
2858
+ }
2859
+
2860
+ /**
2861
+ * Determine whether this DateTime is the same as or after another date.
2862
+ * @param {DateTime} other The date to compare to.
2863
+ * @return {Boolean} TRUE if this DateTime is the same as or after the other date, otherwise FALSE.
2864
+ */
2865
+ function isSameOrAfter(other) {
2866
+ return this.diff(other) >= 0;
2867
+ }
2868
+
2869
+ /**
2870
+ * Determine whether this DateTime is the same as or after another date (comparing by day).
2871
+ * @param {DateTime} other The date to compare to.
2872
+ * @return {Boolean} TRUE if this DateTime is the same as or after the other date (comparing by day), otherwise FALSE.
2873
+ */
2874
+ function isSameOrAfterDay(other) {
2875
+ return this.diffInDays(other) >= 0;
2876
+ }
2877
+
2878
+ /**
2879
+ * Determine whether this DateTime is the same as or after another date (comparing by hour).
2880
+ * @param {DateTime} other The date to compare to.
2881
+ * @return {Boolean} TRUE if this DateTime is the same as or after the other date (comparing by hour), otherwise FALSE.
2882
+ */
2883
+ function isSameOrAfterHour(other) {
2884
+ return this.diffInHours(other) >= 0;
2885
+ }
2886
+
2887
+ /**
2888
+ * Determine whether this DateTime is the same as or after another date (comparing by minute).
2889
+ * @param {DateTime} other The date to compare to.
2890
+ * @return {Boolean} TRUE if this DateTime is the same as or after the other date (comparing by minute), otherwise FALSE.
2891
+ */
2892
+ function isSameOrAfterMinute(other) {
2893
+ return this.diffInMinutes(other) >= 0;
2894
+ }
2895
+
2896
+ /**
2897
+ * Determine whether this DateTime is the same as or after another date (comparing by month).
2898
+ * @param {DateTime} other The date to compare to.
2899
+ * @return {Boolean} TRUE if this DateTime is the same as or after the other date (comparing by month), otherwise FALSE.
2900
+ */
2901
+ function isSameOrAfterMonth(other) {
2902
+ return this.diffInMonths(other) >= 0;
2903
+ }
2904
+
2905
+ /**
2906
+ * Determine whether this DateTime is the same as or after another date (comparing by second).
2907
+ * @param {DateTime} other The date to compare to.
2908
+ * @return {Boolean} TRUE if this DateTime is the same as or after the other date (comparing by second), otherwise FALSE.
2909
+ */
2910
+ function isSameOrAfterSecond(other) {
2911
+ return this.diffInSeconds(other) >= 0;
2912
+ }
2913
+
2914
+ /**
2915
+ * Determine whether this DateTime is the same as or after another date (comparing by week).
2916
+ * @param {DateTime} other The date to compare to.
2917
+ * @return {Boolean} TRUE if this DateTime is the same as or after the other date (comparing by week), otherwise FALSE.
2918
+ */
2919
+ function isSameOrAfterWeek(other) {
2920
+ return this.diffInWeeks(other) >= 0;
2921
+ }
2922
+
2923
+ /**
2924
+ * Determine whether this DateTime is the same as or after another date (comparing by year).
2925
+ * @param {DateTime} other The date to compare to.
2926
+ * @return {Boolean} TRUE if this DateTime is the same as or after the other date (comparing by year), otherwise FALSE.
2927
+ */
2928
+ function isSameOrAfterYear(other) {
2929
+ return this.diffInYears(other) >= 0;
2930
+ }
2931
+
2932
+ /**
2933
+ * Determine whether this DateTime is the same as or before another date.
2934
+ * @param {DateTime} other The date to compare to.
2935
+ * @return {Boolean} TRUE if this DateTime is the same as or before the other date, otherwise FALSE.
2936
+ */
2937
+ function isSameOrBefore(other) {
2938
+ return this.diff(other) <= 0;
2939
+ }
2940
+
2941
+ /**
2942
+ * Determine whether this DateTime is the same as or before another date (comparing by day).
2943
+ * @param {DateTime} other The date to compare to.
2944
+ * @return {Boolean} TRUE if this DateTime is the same as or before the other date (comparing by day), otherwise FALSE.
2945
+ */
2946
+ function isSameOrBeforeDay(other) {
2947
+ return this.diffInDays(other) <= 0;
2948
+ }
2949
+
2950
+ /**
2951
+ * Determine whether this DateTime is the same as or before another date (comparing by hour).
2952
+ * @param {DateTime} other The date to compare to.
2953
+ * @return {Boolean} TRUE if this DateTime is the same as or before the other date (comparing by hour), otherwise FALSE.
2954
+ */
2955
+ function isSameOrBeforeHour(other) {
2956
+ return this.diffInHours(other) <= 0;
2957
+ }
2958
+
2959
+ /**
2960
+ * Determine whether this DateTime is the same as or before another date (comparing by minute).
2961
+ * @param {DateTime} other The date to compare to.
2962
+ * @return {Boolean} TRUE if this DateTime is the same as or before the other date (comparing by minute), otherwise FALSE.
2963
+ */
2964
+ function isSameOrBeforeMinute(other) {
2965
+ return this.diffInMinutes(other) <= 0;
2966
+ }
2967
+
2968
+ /**
2969
+ * Determine whether this DateTime is the same as or before another date (comparing by month).
2970
+ * @param {DateTime} other The date to compare to.
2971
+ * @return {Boolean} TRUE if this DateTime is the same as or before the other date (comparing by month), otherwise FALSE.
2972
+ */
2973
+ function isSameOrBeforeMonth(other) {
2974
+ return this.diffInMonths(other) <= 0;
2975
+ }
2976
+
2977
+ /**
2978
+ * Determine whether this DateTime is the same as or before another date (comparing by second).
2979
+ * @param {DateTime} other The date to compare to.
2980
+ * @return {Boolean} TRUE if this DateTime is the same as or before the other date (comparing by second), otherwise FALSE.
2981
+ */
2982
+ function isSameOrBeforeSecond(other) {
2983
+ return this.diffInSeconds(other) <= 0;
2984
+ }
2985
+
2986
+ /**
2987
+ * Determine whether this DateTime is the same as or before another date (comparing by week).
2988
+ * @param {DateTime} other The date to compare to.
2989
+ * @return {Boolean} TRUE if this DateTime is the same as or before the other date (comparing by week), otherwise FALSE.
2990
+ */
2991
+ function isSameOrBeforeWeek(other) {
2992
+ return this.diffInWeeks(other) <= 0;
2993
+ }
2994
+
2995
+ /**
2996
+ * Determine whether this DateTime is the same as or before another date (comparing by year).
2997
+ * @param {DateTime} other The date to compare to.
2998
+ * @return {Boolean} TRUE if this DateTime is the same as or before the other date (comparing by year), otherwise FALSE.
2999
+ */
3000
+ function isSameOrBeforeYear(other) {
3001
+ return this.diffInYears(other) <= 0;
3002
+ }
3003
+
3004
+ /**
3005
+ * DateTime Manipulation
3006
+ */
3007
+
3008
+ /**
3009
+ * Add a day to the current DateTime.
3010
+ * @return {DateTime} The DateTime object.
3011
+ */
3012
+ function addDay() {
3013
+ return this.addDays(1);
3014
+ }
3015
+ /**
3016
+ * Add days to the current DateTime.
3017
+ * @param {number} amount The number of days to add.
3018
+ * @return {DateTime} The DateTime object.
3019
+ */
3020
+ function addDays(amount) {
3021
+ return this.setDate(
3022
+ this.getDate() + amount,
3023
+ );
3024
+ }
3025
+ /**
3026
+ * Add an hour to the current DateTime.
3027
+ * @return {DateTime} The DateTime object.
3028
+ */
3029
+ function addHour() {
3030
+ return this.addHours(1);
3031
+ }
3032
+ /**
3033
+ * Add hours to the current DateTime.
3034
+ * @param {number} amount The number of hours to add.
3035
+ * @return {DateTime} The DateTime object.
3036
+ */
3037
+ function addHours(amount) {
3038
+ return this.setTime(
3039
+ this.getTime() + (amount * 3600000),
3040
+ );
3041
+ }
3042
+ /**
3043
+ * Add a minute to the current DateTime.
3044
+ * @return {DateTime} The DateTime object.
3045
+ */
3046
+ function addMinute() {
3047
+ return this.addMinutes(1);
3048
+ }
3049
+ /**
3050
+ * Add minutes to the current DateTime.
3051
+ * @param {number} amount The number of minutes to add.
3052
+ * @return {DateTime} The DateTime object.
3053
+ */
3054
+ function addMinutes(amount) {
3055
+ return this.setTime(
3056
+ this.getTime() + (amount * 60000),
3057
+ );
3058
+ }
3059
+ /**
3060
+ * Add a month to the current DateTime.
3061
+ * @return {DateTime} The DateTime object.
3062
+ */
3063
+ function addMonth() {
3064
+ return this.addMonths(1);
3065
+ }
3066
+ /**
3067
+ * Add months to the current DateTime.
3068
+ * @param {number} amount The number of months to add.
3069
+ * @return {DateTime} The DateTime object.
3070
+ */
3071
+ function addMonths(amount) {
3072
+ return this.setMonth(
3073
+ this.getMonth() + amount,
2542
3074
  );
2543
3075
  }
2544
3076
  /**
2545
- * Get the number of days in the current year.
2546
- * @return {number} The number of days in the current year.
3077
+ * Add a second to the current DateTime.
3078
+ * @return {DateTime} The DateTime object.
3079
+ */
3080
+ function addSecond() {
3081
+ return this.addSeconds(1);
3082
+ }
3083
+ /**
3084
+ * Add seconds to the current DateTime.
3085
+ * @param {number} amount The number of seconds to add.
3086
+ * @return {DateTime} The DateTime object.
3087
+ */
3088
+ function addSeconds(amount) {
3089
+ return this.setTime(
3090
+ this.getTime() + (amount * 1000),
3091
+ );
3092
+ }
3093
+ /**
3094
+ * Add a week to the current DateTime.
3095
+ * @return {DateTime} The DateTime object.
3096
+ */
3097
+ function addWeek() {
3098
+ return this.addWeeks(1);
3099
+ }
3100
+ /**
3101
+ * Add weeks to the current DateTime.
3102
+ * @param {number} amount The number of weeks to add.
3103
+ * @return {DateTime} The DateTime object.
3104
+ */
3105
+ function addWeeks(amount) {
3106
+ return this.setDate(
3107
+ this.getDate() + (amount * 7),
3108
+ );
3109
+ }
3110
+ /**
3111
+ * Add a year to the current DateTime.
3112
+ * @return {DateTime} The DateTime object.
3113
+ */
3114
+ function addYear() {
3115
+ return this.addYears(1);
3116
+ }
3117
+ /**
3118
+ * Add years to the current DateTime.
3119
+ * @param {number} amount The number of years to add.
3120
+ * @return {DateTime} The DateTime object.
3121
+ */
3122
+ function addYears(amount) {
3123
+ return this.setYear(
3124
+ this.getYear() + amount,
3125
+ );
3126
+ }
3127
+ /**
3128
+ * Set the DateTime to the end of the day.
3129
+ * @return {DateTime} The DateTime object.
3130
+ */
3131
+ function endOfDay() {
3132
+ return this.setHours(23, 59, 59, 999);
3133
+ }
3134
+ /**
3135
+ * Set the DateTime to the end of the hour.
3136
+ * @return {DateTime} The DateTime object.
3137
+ */
3138
+ function endOfHour() {
3139
+ return this.setMinutes(59, 59, 999);
3140
+ }
3141
+ /**
3142
+ * Set the DateTime to the end of the minute.
3143
+ * @return {DateTime} The DateTime object.
3144
+ */
3145
+ function endOfMinute() {
3146
+ return this.setSeconds(59, 999);
3147
+ }
3148
+ /**
3149
+ * Set the DateTime to the end of the month.
3150
+ * @return {DateTime} The DateTime object.
3151
+ */
3152
+ function endOfMonth() {
3153
+ return this.setDate(this.daysInMonth())
3154
+ .endOfDay();
3155
+ }
3156
+
3157
+ /**
3158
+ * Set the DateTime to the end of the quarter.
3159
+ * @return {DateTime} The DateTime object.
3160
+ */
3161
+ function endOfQuarter() {
3162
+ const month = this.getQuarter() * 3;
3163
+ return this.setMonth(month, daysInMonth$1(this.getYear(), month))
3164
+ .endOfDay();
3165
+ }
3166
+ /**
3167
+ * Set the DateTime to the end of the second.
3168
+ * @return {DateTime} The DateTime object.
3169
+ */
3170
+ function endOfSecond() {
3171
+ return this.setMilliseconds(999);
3172
+ }
3173
+ /**
3174
+ * Set the DateTime to the end of the week.
3175
+ * @return {DateTime} The DateTime object.
3176
+ */
3177
+ function endOfWeek() {
3178
+ return this.setWeekDay(7)
3179
+ .endOfDay();
3180
+ }
3181
+ /**
3182
+ * Set the DateTime to the end of the year.
3183
+ * @return {DateTime} The DateTime object.
3184
+ */
3185
+ function endOfYear() {
3186
+ return this.setMonth(12, 31)
3187
+ .endOfDay();
3188
+ }
3189
+ /**
3190
+ * Set the DateTime to the start of the day.
3191
+ * @return {DateTime} The DateTime object.
3192
+ */
3193
+ function startOfDay() {
3194
+ return this.setHours(0, 0, 0, 0);
3195
+ }
3196
+ /**
3197
+ * Set the DateTime to the start of the hour.
3198
+ * @return {DateTime} The DateTime object.
3199
+ */
3200
+ function startOfHour() {
3201
+ return this.setMinutes(0, 0, 0);
3202
+ }
3203
+ /**
3204
+ * Set the DateTime to the start of the minute.
3205
+ * @return {DateTime} The DateTime object.
3206
+ */
3207
+ function startOfMinute() {
3208
+ return this.setSeconds(0, 0);
3209
+ }
3210
+ /**
3211
+ * Set the DateTime to the start of the month.
3212
+ * @return {DateTime} The DateTime object.
3213
+ */
3214
+ function startOfMonth() {
3215
+ return this.setDate(1)
3216
+ .startOfDay();
3217
+ }
3218
+
3219
+ /**
3220
+ * Set the DateTime to the start of the quarter.
3221
+ * @return {DateTime} The DateTime object.
3222
+ */
3223
+ function startOfQuarter() {
3224
+ const month = this.getQuarter() * 3 - 2;
3225
+ return this.setMonth(month, 1)
3226
+ .startOfDay();
3227
+ }
3228
+ /**
3229
+ * Set the DateTime to the start of the second.
3230
+ * @return {DateTime} The DateTime object.
3231
+ */
3232
+ function startOfSecond() {
3233
+ return this.setMilliseconds(0);
3234
+ }
3235
+ /**
3236
+ * Set the DateTime to the start of the week.
3237
+ * @return {DateTime} The DateTime object.
3238
+ */
3239
+ function startOfWeek() {
3240
+ return this.setWeekDay(1)
3241
+ .startOfDay();
3242
+ }
3243
+ /**
3244
+ * Set the DateTime to the start of the year.
3245
+ * @return {DateTime} The DateTime object.
3246
+ */
3247
+ function startOfYear() {
3248
+ return this.setMonth(1, 1)
3249
+ .startOfDay();
3250
+ }
3251
+ /**
3252
+ * Subtract a day from the current DateTime.
3253
+ * @return {DateTime} The DateTime object.
3254
+ */
3255
+ function subDay() {
3256
+ return this.addDays(-1);
3257
+ }
3258
+ /**
3259
+ * Subtract days from the current DateTime.
3260
+ * @param {number} amount The number of days to subtract.
3261
+ * @return {DateTime} The DateTime object.
3262
+ */
3263
+ function subDays(amount) {
3264
+ return this.addDays(-amount);
3265
+ }
3266
+ /**
3267
+ * Subtract an hour from the current DateTime.
3268
+ * @return {DateTime} The DateTime object.
3269
+ */
3270
+ function subHour() {
3271
+ return this.addHours(-1);
3272
+ }
3273
+ /**
3274
+ * Subtract hours from the current DateTime.
3275
+ * @param {number} amount The number of hours to subtract.
3276
+ * @return {DateTime} The DateTime object.
3277
+ */
3278
+ function subHours(amount) {
3279
+ return this.addHours(-amount);
3280
+ }
3281
+ /**
3282
+ * Subtract a minute from the current DateTime.
3283
+ * @return {DateTime} The DateTime object.
3284
+ */
3285
+ function subMinute() {
3286
+ return this.addMinutes(-1);
3287
+ }
3288
+ /**
3289
+ * Subtract minutes from the current DateTime.
3290
+ * @param {number} amount The number of minutes to subtract.
3291
+ * @return {DateTime} The DateTime object.
3292
+ */
3293
+ function subMinutes(amount) {
3294
+ return this.addMinutes(-amount);
3295
+ }
3296
+ /**
3297
+ * Subtract a month from the current DateTime.
3298
+ * @return {DateTime} The DateTime object.
2547
3299
  */
2548
- function daysInYear() {
2549
- return daysInYear$1(
2550
- this.getYear(),
2551
- );
3300
+ function subMonth() {
3301
+ return this.addMonths(-1);
2552
3302
  }
2553
3303
  /**
2554
- * Get the difference between this and another Date.
2555
- * @param {DateTime} [other] The date to compare to.
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.
2559
- * @return {number} The difference.
3304
+ * Subtract months from the current DateTime.
3305
+ * @param {number} amount The number of months to subtract.
3306
+ * @return {DateTime} The DateTime object.
2560
3307
  */
2561
- function diff(other, { timeUnit, relative = true } = {}) {
2562
- if (!other) {
2563
- other = new this.constructor;
2564
- }
3308
+ function subMonths(amount) {
3309
+ return this.addMonths(-amount);
3310
+ }
3311
+ /**
3312
+ * Subtract a second from the current DateTime.
3313
+ * @return {DateTime} The DateTime object.
3314
+ */
3315
+ function subSecond() {
3316
+ return this.addSeconds(-1);
3317
+ }
3318
+ /**
3319
+ * Subtract seconds from the current DateTime.
3320
+ * @param {number} amount The number of seconds to subtract.
3321
+ * @return {DateTime} The DateTime object.
3322
+ */
3323
+ function subSeconds(amount) {
3324
+ return this.addSeconds(-amount);
3325
+ }
3326
+ /**
3327
+ * Subtract a week from the current DateTime.
3328
+ * @return {DateTime} The DateTime object.
3329
+ */
3330
+ function subWeek() {
3331
+ return this.addWeeks(-1);
3332
+ }
3333
+ /**
3334
+ * Subtract weeks from the current DateTime.
3335
+ * @param {number} amount The number of weeks to subtract.
3336
+ * @return {DateTime} The DateTime object.
3337
+ */
3338
+ function subWeeks(amount) {
3339
+ return this.addWeeks(-amount);
3340
+ }
3341
+ /**
3342
+ * Subtract a year from the current DateTime.
3343
+ * @return {DateTime} The DateTime object.
3344
+ */
3345
+ function subYear() {
3346
+ return this.addYears(-1);
3347
+ }
3348
+ /**
3349
+ * Subtract years from the current DateTime.
3350
+ * @param {number} amount The number of years to subtract.
3351
+ * @return {DateTime} The DateTime object.
3352
+ */
3353
+ function subYears(amount) {
3354
+ return this.addYears(-amount);
3355
+ }
2565
3356
 
2566
- if (!timeUnit) {
2567
- return this - other;
2568
- }
3357
+ /**
3358
+ * DateTime Output
3359
+ */
2569
3360
 
2570
- if (timeUnit) {
2571
- timeUnit = timeUnit.toLowerCase();
2572
- }
3361
+ /**
3362
+ * Format the current date using a format string.
3363
+ * @param {string} formatString The format string.
3364
+ * @return {string} The formatted date string.
3365
+ */
3366
+ function format(formatString) {
3367
+ let match;
3368
+ let output = '';
2573
3369
 
2574
- other = other.setTimeZone(this.getTimeZone());
3370
+ while (formatString && (match = formatString.match(formatTokenRegExp))) {
3371
+ const token = match[1];
3372
+ const position = match.index;
3373
+ const length = match[0].length;
2575
3374
 
2576
- switch (timeUnit) {
2577
- case 'year':
2578
- case 'years':
2579
- const yearDiff = this.getYear() - other.getYear();
2580
- return compensateDiff(
2581
- this,
2582
- other.setYear(
2583
- this.getYear(),
2584
- ),
2585
- yearDiff,
2586
- !relative,
2587
- -1,
2588
- );
2589
- case 'month':
2590
- case 'months':
2591
- const monthDiff = (this.getYear() - other.getYear()) *
2592
- 12 +
2593
- this.getMonth() -
2594
- other.getMonth();
2595
- return compensateDiff(
2596
- this,
2597
- other.setYear(
2598
- this.getYear(),
2599
- this.getMonth(),
2600
- ),
2601
- monthDiff,
2602
- !relative,
2603
- -1,
2604
- );
2605
- case 'week':
2606
- case 'weeks':
2607
- const weekDiff = (this - other) / 604800000;
2608
- return compensateDiff(
2609
- this,
2610
- other.setWeekYear(
2611
- this.getWeekYear(),
2612
- this.getWeek(),
2613
- ),
2614
- weekDiff,
2615
- relative,
2616
- );
2617
- case 'day':
2618
- case 'days':
2619
- const dayDiff = (this - other) / 86400000;
2620
- return compensateDiff(
2621
- this,
2622
- other.setYear(
2623
- this.getYear(),
2624
- this.getMonth(),
2625
- this.getDate(),
2626
- ),
2627
- dayDiff,
2628
- relative,
2629
- );
2630
- case 'hour':
2631
- case 'hours':
2632
- const hourDiff = (this - other) / 3600000;
2633
- return compensateDiff(
2634
- this,
2635
- other.setYear(
2636
- this.getYear(),
2637
- this.getMonth(),
2638
- this.getDate(),
2639
- ).setHours(
2640
- this.getHours(),
2641
- ),
2642
- hourDiff,
2643
- relative,
2644
- );
2645
- case 'minute':
2646
- case 'minutes':
2647
- const minuteDiff = (this - other) / 60000;
2648
- return compensateDiff(
2649
- this,
2650
- other.setYear(
2651
- this.getYear(),
2652
- this.getMonth(),
2653
- this.getDate(),
2654
- ).setHours(
2655
- this.getHours(),
2656
- this.getMinutes(),
2657
- ),
2658
- minuteDiff,
2659
- relative,
2660
- );
2661
- case 'second':
2662
- case 'seconds':
2663
- const secondDiff = (this - other) / 1000;
2664
- return compensateDiff(
2665
- this,
2666
- other.setYear(
2667
- this.getYear(),
2668
- this.getMonth(),
2669
- this.getDate(),
2670
- ).setHours(
2671
- this.getHours(),
2672
- this.getMinutes(),
2673
- this.getSeconds(),
2674
- ),
2675
- secondDiff,
2676
- relative,
2677
- );
2678
- default:
2679
- throw new Error('Invalid time unit supplied');
3375
+ if (position) {
3376
+ output += formatString.substring(0, position);
3377
+ }
3378
+
3379
+ formatString = formatString.substring(position + length);
3380
+
3381
+ if (!token) {
3382
+ output += match[0].slice(1, -1);
3383
+ continue;
3384
+ }
3385
+
3386
+ if (!(token in tokens)) {
3387
+ throw new Error(`Invalid token in DateTime format: ${token}`);
3388
+ }
3389
+
3390
+ output += tokens[token].output(this, length);
2680
3391
  }
3392
+
3393
+ output += formatString;
3394
+
3395
+ return output;
2681
3396
  }
2682
3397
  /**
2683
- * Get the era in current timeZone.
2684
- * @param {string} [type=long] The type of era to return.
2685
- * @return {string} The era.
3398
+ * Format the current date using "eee MMM dd yyyy".
3399
+ * @return {string} The formatted date string.
2686
3400
  */
2687
- function era(type = 'long') {
2688
- return formatEra(
2689
- this.getLocale(),
2690
- this.getYear() < 0 ?
2691
- 0 :
2692
- 1,
2693
- type,
2694
- );
3401
+ function toDateString() {
3402
+ return this.format(formats.date);
2695
3403
  }
2696
3404
  /**
2697
- * Get the difference between this and another Date in human readable form.
2698
- * @param {DateTime} [other] The date to compare to.
2699
- * @param {object} [options] The options for comparing the dates.
2700
- * @param {string} [options.timeUnit] The unit of time.
2701
- * @return {string} The difference in human readable form.
3405
+ * Format the current date using "yyyy-MM-dd'THH:mm:ss.SSSSSSxxx".
3406
+ * @return {string} The formatted date string.
2702
3407
  */
2703
- function humanDiff(other, { timeUnit } = {}) {
2704
- const relativeFormatter = getRelativeFormatter(this.getLocale());
2705
-
2706
- if (!relativeFormatter) {
2707
- throw new Error('RelativeTimeFormat not supported');
2708
- }
2709
-
2710
- if (!other) {
2711
- other = new this.constructor;
2712
- }
3408
+ function toISOString() {
3409
+ return this
3410
+ .setLocale('en')
3411
+ .setTimeZone('UTC')
3412
+ .format(formats.rfc3339_extended);
3413
+ }
3414
+ /**
3415
+ * Format the current date using "eee MMM dd yyyy HH:mm:ss xx (VV)".
3416
+ * @return {string} The formatted date string.
3417
+ */
3418
+ function toString() {
3419
+ return this.format(formats.string);
3420
+ }
3421
+ /**
3422
+ * Format the current date using "HH:mm:ss xx (VV)".
3423
+ * @return {string} The formatted date string.
3424
+ */
3425
+ function toTimeString() {
3426
+ return this.format(formats.time);
3427
+ }
3428
+ /**
3429
+ * Format the current date in UTC timeZone using "eee MMM dd yyyy HH:mm:ss xx (VV)".
3430
+ * @return {string} The formatted date string.
3431
+ */
3432
+ function toUTCString() {
3433
+ return this
3434
+ .setLocale('en')
3435
+ .setTimeZone('UTC')
3436
+ .toString();
3437
+ }
2713
3438
 
2714
- let amount;
2715
- if (timeUnit) {
2716
- amount = this.diff(other, { timeUnit });
2717
- } else {
2718
- [amount, timeUnit] = getBiggestDiff(this, other);
2719
- }
3439
+ /**
3440
+ * DateTime Utility
3441
+ */
2720
3442
 
2721
- return relativeFormatter.format(amount, timeUnit);
3443
+ /**
3444
+ * Get the name of the day of the week in current timeZone.
3445
+ * @param {string} [type=long] The type of day name to return.
3446
+ * @return {string} The name of the day of the week.
3447
+ */
3448
+ function dayName(type = 'long') {
3449
+ return formatDay(this.getLocale(), this.getDay(), type);
2722
3450
  }
2723
3451
  /**
2724
- * Determine whether this DateTime is after another date (optionally to a granularity).
2725
- * @param {DateTime} [other] The date to compare to.
2726
- * @param {object} [options] The options for comparing the dates.
2727
- * @param {string} [options.granularity] The level of granularity to use for comparison.
2728
- * @return {Boolean} TRUE if this DateTime is after the other date, otherwise FALSE.
3452
+ * Get the day period in current timeZone.
3453
+ * @param {string} [type=long] The type of day period to return.
3454
+ * @return {string} The day period.
2729
3455
  */
2730
- function isAfter(other, { granularity } = {}) {
2731
- return this.diff(other, { timeUnit: granularity }) > 0;
3456
+ function dayPeriod(type = 'long') {
3457
+ return formatDayPeriod(
3458
+ this.getLocale(),
3459
+ this.getHours() < 12 ?
3460
+ 0 :
3461
+ 1,
3462
+ type,
3463
+ );
2732
3464
  }
2733
3465
  /**
2734
- * Determine whether this DateTime is before another date (optionally to a granularity).
2735
- * @param {DateTime} [other] The date to compare to.
2736
- * @param {object} [options] The options for comparing the dates.
2737
- * @param {string} [options.granularity] The level of granularity to use for comparison.
2738
- * @return {Boolean} TRUE if this DateTime is before the other date, otherwise FALSE.
3466
+ * Get the number of days in the current month.
3467
+ * @return {number} The number of days in the current month.
2739
3468
  */
2740
- function isBefore(other, { granularity } = {}) {
2741
- return this.diff(other, { timeUnit: granularity }) < 0;
3469
+ function daysInMonth() {
3470
+ return daysInMonth$1(
3471
+ this.getYear(),
3472
+ this.getMonth(),
3473
+ );
2742
3474
  }
2743
3475
  /**
2744
- * Determine whether this DateTime is between two other dates (optionally to a granularity).
2745
- * @param {DateTime} [start] The first date to compare to.
2746
- * @param {DateTime} [end] The second date to compare to.
2747
- * @param {object} [options] The options for comparing the dates.
2748
- * @param {string} [options.granularity] The level of granularity to use for comparison.
2749
- * @return {Boolean} TRUE if this DateTime is between the other dates, otherwise FALSE.
3476
+ * Get the number of days in the current year.
3477
+ * @return {number} The number of days in the current year.
2750
3478
  */
2751
- function isBetween(start, end, { granularity } = {}) {
2752
- return this.diff(start, { timeUnit: granularity }) > 0 && this.diff(end, { timeUnit: granularity }) < 0;
3479
+ function daysInYear() {
3480
+ return daysInYear$1(
3481
+ this.getYear(),
3482
+ );
3483
+ }
3484
+ /**
3485
+ * Get the era in current timeZone.
3486
+ * @param {string} [type=long] The type of era to return.
3487
+ * @return {string} The era.
3488
+ */
3489
+ function era(type = 'long') {
3490
+ return formatEra(
3491
+ this.getLocale(),
3492
+ this.getYear() < 0 ?
3493
+ 0 :
3494
+ 1,
3495
+ type,
3496
+ );
2753
3497
  }
2754
3498
  /**
2755
3499
  * Return true if the DateTime is in daylight savings.
@@ -2779,36 +3523,6 @@
2779
3523
  this.getYear(),
2780
3524
  );
2781
3525
  }
2782
- /**
2783
- * Determine whether this DateTime is the same as another date (optionally to a granularity).
2784
- * @param {DateTime} [other] The date to compare to.
2785
- * @param {object} [options] The options for comparing the dates.
2786
- * @param {string} [options.granularity] The level of granularity to use for comparison.
2787
- * @return {Boolean} TRUE if this DateTime is the same as the other date, otherwise FALSE.
2788
- */
2789
- function isSame(other, { granularity } = {}) {
2790
- return this.diff(other, { timeUnit: granularity }) === 0;
2791
- }
2792
- /**
2793
- * Determine whether this DateTime is the same or after another date (optionally to a granularity).
2794
- * @param {DateTime} [other] The date to compare to.
2795
- * @param {object} [options] The options for comparing the dates.
2796
- * @param {string} [options.granularity] The level of granularity to use for comparison.
2797
- * @return {Boolean} TRUE if this DateTime is the same or after the other date, otherwise FALSE.
2798
- */
2799
- function isSameOrAfter(other, { granularity } = {}) {
2800
- return this.diff(other, { timeUnit: granularity }) >= 0;
2801
- }
2802
- /**
2803
- * Determine whether this DateTime is the same or before another date.
2804
- * @param {DateTime} other The date to compare to.
2805
- * @param {object} [options] The options for comparing the dates.
2806
- * @param {string} [options.granularity] The level of granularity to use for comparison.
2807
- * @return {Boolean} TRUE if this DateTime is the same or before the other date, otherwise FALSE.
2808
- */
2809
- function isSameOrBefore(other, { granularity } = {}) {
2810
- return this.diff(other, { timeUnit: granularity }) <= 0;
2811
- }
2812
3526
  /**
2813
3527
  * Get the name of the month in current timeZone.
2814
3528
  * @param {string} [type=long] The type of month name to return.
@@ -2854,13 +3568,40 @@
2854
3568
 
2855
3569
  const proto = DateTime.prototype;
2856
3570
 
2857
- proto.add = add;
3571
+ proto.addDay = addDay;
3572
+ proto.addDays = addDays;
3573
+ proto.addHour = addHour;
3574
+ proto.addHours = addHours;
3575
+ proto.addMinute = addMinute;
3576
+ proto.addMinutes = addMinutes;
3577
+ proto.addMonth = addMonth;
3578
+ proto.addMonths = addMonths;
3579
+ proto.addSecond = addSecond;
3580
+ proto.addSeconds = addSeconds;
3581
+ proto.addWeek = addWeek;
3582
+ proto.addWeeks = addWeeks;
3583
+ proto.addYear = addYear;
3584
+ proto.addYears = addYears;
2858
3585
  proto.dayName = dayName;
2859
3586
  proto.dayPeriod = dayPeriod;
2860
3587
  proto.daysInMonth = daysInMonth;
2861
3588
  proto.daysInYear = daysInYear;
2862
3589
  proto.diff = diff;
2863
- proto.endOf = endOf;
3590
+ proto.diffInDays = diffInDays;
3591
+ proto.diffInHours = diffInHours;
3592
+ proto.diffInMinutes = diffInMinutes;
3593
+ proto.diffInMonths = diffInMonths;
3594
+ proto.diffInSeconds = diffInSeconds;
3595
+ proto.diffInWeeks = diffInWeeks;
3596
+ proto.diffInYears = diffInYears;
3597
+ proto.endOfDay = endOfDay;
3598
+ proto.endOfHour = endOfHour;
3599
+ proto.endOfMinute = endOfMinute;
3600
+ proto.endOfMonth = endOfMonth;
3601
+ proto.endOfQuarter = endOfQuarter;
3602
+ proto.endOfSecond = endOfSecond;
3603
+ proto.endOfWeek = endOfWeek;
3604
+ proto.endOfYear = endOfYear;
2864
3605
  proto.era = era;
2865
3606
  proto.format = format;
2866
3607
  proto.getDate = getDate;
@@ -2880,14 +3621,63 @@
2880
3621
  proto.getWeekYear = getWeekYear;
2881
3622
  proto.getYear = getYear;
2882
3623
  proto.humanDiff = humanDiff;
3624
+ proto.humanDiffInDays = humanDiffInDays;
3625
+ proto.humanDiffInHours = humanDiffInHours;
3626
+ proto.humanDiffInMinutes = humanDiffInMinutes;
3627
+ proto.humanDiffInMonths = humanDiffInMonths;
3628
+ proto.humanDiffInSeconds = humanDiffInSeconds;
3629
+ proto.humanDiffInWeeks = humanDiffInWeeks;
3630
+ proto.humanDiffInYears = humanDiffInYears;
2883
3631
  proto.isAfter = isAfter;
3632
+ proto.isAfterDay = isAfterDay;
3633
+ proto.isAfterHour = isAfterHour;
3634
+ proto.isAfterMinute = isAfterMinute;
3635
+ proto.isAfterMonth = isAfterMonth;
3636
+ proto.isAfterSecond = isAfterSecond;
3637
+ proto.isAfterWeek = isAfterWeek;
3638
+ proto.isAfterYear = isAfterYear;
2884
3639
  proto.isBefore = isBefore;
3640
+ proto.isBeforeDay = isBeforeDay;
3641
+ proto.isBeforeHour = isBeforeHour;
3642
+ proto.isBeforeMinute = isBeforeMinute;
3643
+ proto.isBeforeMonth = isBeforeMonth;
3644
+ proto.isBeforeSecond = isBeforeSecond;
3645
+ proto.isBeforeWeek = isBeforeWeek;
3646
+ proto.isBeforeYear = isBeforeYear;
2885
3647
  proto.isBetween = isBetween;
3648
+ proto.isBetweenDay = isBetweenDay;
3649
+ proto.isBetweenHour = isBetweenHour;
3650
+ proto.isBetweenMinute = isBetweenMinute;
3651
+ proto.isBetweenMonth = isBetweenMonth;
3652
+ proto.isBetweenSecond = isBetweenSecond;
3653
+ proto.isBetweenWeek = isBetweenWeek;
3654
+ proto.isBetweenYear = isBetweenYear;
2886
3655
  proto.isDST = isDST;
2887
3656
  proto.isLeapYear = isLeapYear;
2888
3657
  proto.isSame = isSame;
3658
+ proto.isSameDay = isSameDay;
3659
+ proto.isSameHour = isSameHour;
3660
+ proto.isSameMinute = isSameMinute;
3661
+ proto.isSameMonth = isSameMonth;
3662
+ proto.isSameSecond = isSameSecond;
3663
+ proto.isSameWeek = isSameWeek;
3664
+ proto.isSameYear = isSameYear;
2889
3665
  proto.isSameOrAfter = isSameOrAfter;
3666
+ proto.isSameOrAfterDay = isSameOrAfterDay;
3667
+ proto.isSameOrAfterHour = isSameOrAfterHour;
3668
+ proto.isSameOrAfterMinute = isSameOrAfterMinute;
3669
+ proto.isSameOrAfterMonth = isSameOrAfterMonth;
3670
+ proto.isSameOrAfterSecond = isSameOrAfterSecond;
3671
+ proto.isSameOrAfterWeek = isSameOrAfterWeek;
3672
+ proto.isSameOrAfterYear = isSameOrAfterYear;
2890
3673
  proto.isSameOrBefore = isSameOrBefore;
3674
+ proto.isSameOrBeforeDay = isSameOrBeforeDay;
3675
+ proto.isSameOrBeforeHour = isSameOrBeforeHour;
3676
+ proto.isSameOrBeforeMinute = isSameOrBeforeMinute;
3677
+ proto.isSameOrBeforeMonth = isSameOrBeforeMonth;
3678
+ proto.isSameOrBeforeSecond = isSameOrBeforeSecond;
3679
+ proto.isSameOrBeforeWeek = isSameOrBeforeWeek;
3680
+ proto.isSameOrBeforeYear = isSameOrBeforeYear;
2891
3681
  proto.monthName = monthName;
2892
3682
  proto.setDate = setDate;
2893
3683
  proto.setDay = setDay;
@@ -2905,8 +3695,28 @@
2905
3695
  proto.setWeekOfMonth = setWeekOfMonth;
2906
3696
  proto.setWeekYear = setWeekYear;
2907
3697
  proto.setYear = setYear;
2908
- proto.startOf = startOf;
2909
- proto.sub = sub;
3698
+ proto.startOfDay = startOfDay;
3699
+ proto.startOfHour = startOfHour;
3700
+ proto.startOfMinute = startOfMinute;
3701
+ proto.startOfMonth = startOfMonth;
3702
+ proto.startOfQuarter = startOfQuarter;
3703
+ proto.startOfSecond = startOfSecond;
3704
+ proto.startOfWeek = startOfWeek;
3705
+ proto.startOfYear = startOfYear;
3706
+ proto.subDay = subDay;
3707
+ proto.subDays = subDays;
3708
+ proto.subHour = subHour;
3709
+ proto.subHours = subHours;
3710
+ proto.subMinute = subMinute;
3711
+ proto.subMinutes = subMinutes;
3712
+ proto.subMonth = subMonth;
3713
+ proto.subMonths = subMonths;
3714
+ proto.subSecond = subSecond;
3715
+ proto.subSeconds = subSeconds;
3716
+ proto.subWeek = subWeek;
3717
+ proto.subWeeks = subWeeks;
3718
+ proto.subYear = subYear;
3719
+ proto.subYears = subYears;
2910
3720
  proto.timeZoneName = timeZoneName;
2911
3721
  proto.toDateString = toDateString;
2912
3722
  proto.toISOString = toISOString;