index.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import _typeof from "@babel/runtime/helpers/esm/typeof";
  2. import subDays from "../subDays/index.js";
  3. import subMonths from "../subMonths/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(date, duration) {
  46. requiredArgs(2, arguments);
  47. if (!duration || _typeof(duration) !== 'object') return new Date(NaN);
  48. var years = duration.years ? toInteger(duration.years) : 0;
  49. var months = duration.months ? toInteger(duration.months) : 0;
  50. var weeks = duration.weeks ? toInteger(duration.weeks) : 0;
  51. var days = duration.days ? toInteger(duration.days) : 0;
  52. var hours = duration.hours ? toInteger(duration.hours) : 0;
  53. var minutes = duration.minutes ? toInteger(duration.minutes) : 0;
  54. var seconds = duration.seconds ? toInteger(duration.seconds) : 0;
  55. // Subtract years and months
  56. var dateWithoutMonths = subMonths(date, months + years * 12);
  57. // Subtract weeks and days
  58. var dateWithoutDays = subDays(dateWithoutMonths, days + weeks * 7);
  59. // Subtract hours, minutes and seconds
  60. var minutestoSub = minutes + hours * 60;
  61. var secondstoSub = seconds + minutestoSub * 60;
  62. var mstoSub = secondstoSub * 1000;
  63. var finalDate = new Date(dateWithoutDays.getTime() - mstoSub);
  64. return finalDate;
  65. }