UNPKG

3.16 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = sub;
7
8var _index = _interopRequireDefault(require("../subDays/index.js"));
9
10var _index2 = _interopRequireDefault(require("../subMonths/index.js"));
11
12var _index3 = _interopRequireDefault(require("../toDate/index.js"));
13
14var _index4 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
15
16var _index5 = _interopRequireDefault(require("../_lib/toInteger/index.js"));
17
18function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
20/**
21 * @name sub
22 * @category Common Helpers
23 * @summary Subtract the specified years, months, weeks, days, hours, minutes and seconds from the given date.
24 *
25 * @description
26 * Subtract the specified years, months, weeks, days, hours, minutes and seconds from the given date.
27 *
28 * @param {Date|Number} date - the date to be changed
29 * @param {Duration} duration - the object with years, months, weeks, days, hours, minutes and seconds to be subtracted
30 *
31 * | Key | Description |
32 * |---------|------------------------------------|
33 * | years | Amount of years to be subtracted |
34 * | months | Amount of months to be subtracted |
35 * | weeks | Amount of weeks to be subtracted |
36 * | days | Amount of days to be subtracted |
37 * | hours | Amount of hours to be subtracted |
38 * | minutes | Amount of minutes to be subtracted |
39 * | seconds | Amount of seconds to be subtracted |
40 *
41 * All values default to 0
42 *
43 * @returns {Date} the new date with the seconds subtracted
44 * @throws {TypeError} 2 arguments required
45 *
46 * @example
47 * // Subtract the following duration from 15 June 2017 15:29:20
48 * const result = sub(new Date(2017, 5, 15, 15, 29, 20), {
49 * years: 2,
50 * months: 9,
51 * weeks: 1,
52 * days: 7,
53 * hours: 5,
54 * minutes: 9,
55 * seconds: 30
56 * })
57 * //=> Mon Sep 1 2014 10:19:50
58 */
59function sub(dirtyDate, duration) {
60 (0, _index4.default)(2, arguments);
61 if (!duration || typeof duration !== 'object') return new Date(NaN);
62 var years = 'years' in duration ? (0, _index5.default)(duration.years) : 0;
63 var months = 'months' in duration ? (0, _index5.default)(duration.months) : 0;
64 var weeks = 'weeks' in duration ? (0, _index5.default)(duration.weeks) : 0;
65 var days = 'days' in duration ? (0, _index5.default)(duration.days) : 0;
66 var hours = 'hours' in duration ? (0, _index5.default)(duration.hours) : 0;
67 var minutes = 'minutes' in duration ? (0, _index5.default)(duration.minutes) : 0;
68 var seconds = 'seconds' in duration ? (0, _index5.default)(duration.seconds) : 0; // Subtract years and months
69
70 var dateWithoutMonths = (0, _index2.default)((0, _index3.default)(dirtyDate), months + years * 12); // Subtract weeks and days
71
72 var dateWithoutDays = (0, _index.default)(dateWithoutMonths, days + weeks * 7); // Subtract hours, minutes and seconds
73
74 var minutestoSub = minutes + hours * 60;
75 var secondstoSub = seconds + minutestoSub * 60;
76 var mstoSub = secondstoSub * 1000;
77 var finalDate = new Date(dateWithoutDays.getTime() - mstoSub);
78 return finalDate;
79}
80
81module.exports = exports.default;
\No newline at end of file