index.js 3.0 KB

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