index.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import _typeof from "@babel/runtime/helpers/esm/typeof";
  2. import addDays from "../addDays/index.js";
  3. import addMonths from "../addMonths/index.js";
  4. import toDate from "../toDate/index.js";
  5. import requiredArgs from "../_lib/requiredArgs/index.js";
  6. import toInteger from "../_lib/toInteger/index.js";
  7. /**
  8. * @name add
  9. * @category Common Helpers
  10. * @summary Add the specified years, months, weeks, days, hours, minutes and seconds to the given date.
  11. *
  12. * @description
  13. * Add the specified years, months, weeks, days, hours, minutes and seconds to the given date.
  14. *
  15. * @param {Date|Number} date - the date to be changed
  16. * @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`.
  17. *
  18. * | Key | Description |
  19. * |----------------|------------------------------------|
  20. * | years | Amount of years to be added |
  21. * | months | Amount of months to be added |
  22. * | weeks | Amount of weeks to be added |
  23. * | days | Amount of days to be added |
  24. * | hours | Amount of hours to be added |
  25. * | minutes | Amount of minutes to be added |
  26. * | seconds | Amount of seconds to be added |
  27. *
  28. * All values default to 0
  29. *
  30. * @returns {Date} the new date with the seconds added
  31. * @throws {TypeError} 2 arguments required
  32. *
  33. * @example
  34. * // Add the following duration to 1 September 2014, 10:19:50
  35. * const result = add(new Date(2014, 8, 1, 10, 19, 50), {
  36. * years: 2,
  37. * months: 9,
  38. * weeks: 1,
  39. * days: 7,
  40. * hours: 5,
  41. * minutes: 9,
  42. * seconds: 30,
  43. * })
  44. * //=> Thu Jun 15 2017 15:29:20
  45. */
  46. export default function add(dirtyDate, duration) {
  47. requiredArgs(2, arguments);
  48. if (!duration || _typeof(duration) !== 'object') return new Date(NaN);
  49. var years = duration.years ? toInteger(duration.years) : 0;
  50. var months = duration.months ? toInteger(duration.months) : 0;
  51. var weeks = duration.weeks ? toInteger(duration.weeks) : 0;
  52. var days = duration.days ? toInteger(duration.days) : 0;
  53. var hours = duration.hours ? toInteger(duration.hours) : 0;
  54. var minutes = duration.minutes ? toInteger(duration.minutes) : 0;
  55. var seconds = duration.seconds ? toInteger(duration.seconds) : 0;
  56. // Add years and months
  57. var date = toDate(dirtyDate);
  58. var dateWithMonths = months || years ? addMonths(date, months + years * 12) : date;
  59. // Add weeks and days
  60. var dateWithDays = days || weeks ? addDays(dateWithMonths, days + weeks * 7) : dateWithMonths;
  61. // Add days, hours, minutes and seconds
  62. var minutesToAdd = minutes + hours * 60;
  63. var secondsToAdd = seconds + minutesToAdd * 60;
  64. var msToAdd = secondsToAdd * 1000;
  65. var finalDate = new Date(dateWithDays.getTime() + msToAdd);
  66. return finalDate;
  67. }