index.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = add;
  6. var _index = _interopRequireDefault(require("../addDays/index.js"));
  7. var _index2 = _interopRequireDefault(require("../addMonths/index.js"));
  8. var _index3 = _interopRequireDefault(require("../toDate/index.js"));
  9. var _index4 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  10. var _index5 = _interopRequireDefault(require("../_lib/toInteger/index.js"));
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. /**
  13. * @name add
  14. * @category Common Helpers
  15. * @summary Add the specified years, months, weeks, days, hours, minutes and seconds to the given date.
  16. *
  17. * @description
  18. * Add the specified years, months, weeks, days, hours, minutes and seconds to the given date.
  19. *
  20. * @param {Date|Number} date - the date to be changed
  21. * @param {Duration} duration - the object with years, months, weeks, days, hours, minutes and seconds to be added. Positive decimals will be rounded using `Math.floor`, decimals less than zero will be rounded using `Math.ceil`.
  22. *
  23. * | Key | Description |
  24. * |----------------|------------------------------------|
  25. * | years | Amount of years to be added |
  26. * | months | Amount of months to be added |
  27. * | weeks | Amount of weeks to be added |
  28. * | days | Amount of days to be added |
  29. * | hours | Amount of hours to be added |
  30. * | minutes | Amount of minutes to be added |
  31. * | seconds | Amount of seconds to be added |
  32. *
  33. * All values default to 0
  34. *
  35. * @returns {Date} the new date with the seconds added
  36. * @throws {TypeError} 2 arguments required
  37. *
  38. * @example
  39. * // Add the following duration to 1 September 2014, 10:19:50
  40. * const result = add(new Date(2014, 8, 1, 10, 19, 50), {
  41. * years: 2,
  42. * months: 9,
  43. * weeks: 1,
  44. * days: 7,
  45. * hours: 5,
  46. * minutes: 9,
  47. * seconds: 30,
  48. * })
  49. * //=> Thu Jun 15 2017 15:29:20
  50. */
  51. function add(dirtyDate, duration) {
  52. (0, _index4.default)(2, arguments);
  53. if (!duration || typeof duration !== 'object') return new Date(NaN);
  54. var years = 'years' in duration ? (0, _index5.default)(duration.years) : 0;
  55. var months = 'months' in duration ? (0, _index5.default)(duration.months) : 0;
  56. var weeks = 'weeks' in duration ? (0, _index5.default)(duration.weeks) : 0;
  57. var days = 'days' in duration ? (0, _index5.default)(duration.days) : 0;
  58. var hours = 'hours' in duration ? (0, _index5.default)(duration.hours) : 0;
  59. var minutes = 'minutes' in duration ? (0, _index5.default)(duration.minutes) : 0;
  60. var seconds = 'seconds' in duration ? (0, _index5.default)(duration.seconds) : 0; // Add years and months
  61. var date = (0, _index3.default)(dirtyDate);
  62. var dateWithMonths = months || years ? (0, _index2.default)(date, months + years * 12) : date; // Add weeks and days
  63. var dateWithDays = days || weeks ? (0, _index.default)(dateWithMonths, days + weeks * 7) : dateWithMonths; // Add days, hours, minutes and seconds
  64. var minutesToAdd = minutes + hours * 60;
  65. var secondsToAdd = seconds + minutesToAdd * 60;
  66. var msToAdd = secondsToAdd * 1000;
  67. var finalDate = new Date(dateWithDays.getTime() + msToAdd);
  68. return finalDate;
  69. }
  70. module.exports = exports.default;