index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import subDays from "../subDays/index.js";
  2. import subMonths from "../subMonths/index.js";
  3. import toDate from "../toDate/index.js";
  4. import requiredArgs from "../_lib/requiredArgs/index.js";
  5. import toInteger from "../_lib/toInteger/index.js";
  6. /**
  7. * @name sub
  8. * @category Common Helpers
  9. * @summary Subtract the specified years, months, weeks, days, hours, minutes and seconds from the given date.
  10. *
  11. * @description
  12. * Subtract the specified years, months, weeks, days, hours, minutes and seconds from the given date.
  13. *
  14. * @param {Date|Number} date - the date to be changed
  15. * @param {Duration} duration - the object with years, months, weeks, days, hours, minutes and seconds to be subtracted
  16. *
  17. * | Key | Description |
  18. * |---------|------------------------------------|
  19. * | years | Amount of years to be subtracted |
  20. * | months | Amount of months to be subtracted |
  21. * | weeks | Amount of weeks to be subtracted |
  22. * | days | Amount of days to be subtracted |
  23. * | hours | Amount of hours to be subtracted |
  24. * | minutes | Amount of minutes to be subtracted |
  25. * | seconds | Amount of seconds to be subtracted |
  26. *
  27. * All values default to 0
  28. *
  29. * @returns {Date} the new date with the seconds subtracted
  30. * @throws {TypeError} 2 arguments required
  31. *
  32. * @example
  33. * // Subtract the following duration from 15 June 2017 15:29:20
  34. * const result = sub(new Date(2017, 5, 15, 15, 29, 20), {
  35. * years: 2,
  36. * months: 9,
  37. * weeks: 1,
  38. * days: 7,
  39. * hours: 5,
  40. * minutes: 9,
  41. * seconds: 30
  42. * })
  43. * //=> Mon Sep 1 2014 10:19:50
  44. */
  45. export default function sub(dirtyDate, duration) {
  46. requiredArgs(2, arguments);
  47. if (!duration || typeof duration !== 'object') return new Date(NaN);
  48. var years = 'years' in duration ? toInteger(duration.years) : 0;
  49. var months = 'months' in duration ? toInteger(duration.months) : 0;
  50. var weeks = 'weeks' in duration ? toInteger(duration.weeks) : 0;
  51. var days = 'days' in duration ? toInteger(duration.days) : 0;
  52. var hours = 'hours' in duration ? toInteger(duration.hours) : 0;
  53. var minutes = 'minutes' in duration ? toInteger(duration.minutes) : 0;
  54. var seconds = 'seconds' in duration ? toInteger(duration.seconds) : 0; // Subtract years and months
  55. var dateWithoutMonths = subMonths(toDate(dirtyDate), months + years * 12); // Subtract weeks and days
  56. var dateWithoutDays = subDays(dateWithoutMonths, days + weeks * 7); // Subtract hours, minutes and seconds
  57. var minutestoSub = minutes + hours * 60;
  58. var secondstoSub = seconds + minutestoSub * 60;
  59. var mstoSub = secondstoSub * 1000;
  60. var finalDate = new Date(dateWithoutDays.getTime() - mstoSub);
  61. return finalDate;
  62. }