index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = sub;
  6. var _index = _interopRequireDefault(require("../subDays/index.js"));
  7. var _index2 = _interopRequireDefault(require("../subMonths/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 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(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; // Subtract years and months
  61. var dateWithoutMonths = (0, _index2.default)((0, _index3.default)(dirtyDate), months + years * 12); // Subtract weeks and days
  62. var dateWithoutDays = (0, _index.default)(dateWithoutMonths, days + weeks * 7); // Subtract hours, minutes and seconds
  63. var minutestoSub = minutes + hours * 60;
  64. var secondstoSub = seconds + minutestoSub * 60;
  65. var mstoSub = secondstoSub * 1000;
  66. var finalDate = new Date(dateWithoutDays.getTime() - mstoSub);
  67. return finalDate;
  68. }
  69. module.exports = exports.default;