index.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import addDays from "../addDays/index.js";
  2. import addMonths from "../addMonths/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 add
  8. * @category Common Helpers
  9. * @summary Add the specified years, months, weeks, days, hours, minutes and seconds to the given date.
  10. *
  11. * @description
  12. * Add the specified years, months, weeks, days, hours, minutes and seconds to 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 added. Positive decimals will be rounded using `Math.floor`, decimals less than zero will be rounded using `Math.ceil`.
  16. *
  17. * | Key | Description |
  18. * |----------------|------------------------------------|
  19. * | years | Amount of years to be added |
  20. * | months | Amount of months to be added |
  21. * | weeks | Amount of weeks to be added |
  22. * | days | Amount of days to be added |
  23. * | hours | Amount of hours to be added |
  24. * | minutes | Amount of minutes to be added |
  25. * | seconds | Amount of seconds to be added |
  26. *
  27. * All values default to 0
  28. *
  29. * @returns {Date} the new date with the seconds added
  30. * @throws {TypeError} 2 arguments required
  31. *
  32. * @example
  33. * // Add the following duration to 1 September 2014, 10:19:50
  34. * const result = add(new Date(2014, 8, 1, 10, 19, 50), {
  35. * years: 2,
  36. * months: 9,
  37. * weeks: 1,
  38. * days: 7,
  39. * hours: 5,
  40. * minutes: 9,
  41. * seconds: 30,
  42. * })
  43. * //=> Thu Jun 15 2017 15:29:20
  44. */
  45. export default function add(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; // Add years and months
  55. var date = toDate(dirtyDate);
  56. var dateWithMonths = months || years ? addMonths(date, months + years * 12) : date; // Add weeks and days
  57. var dateWithDays = days || weeks ? addDays(dateWithMonths, days + weeks * 7) : dateWithMonths; // Add days, hours, minutes and seconds
  58. var minutesToAdd = minutes + hours * 60;
  59. var secondsToAdd = seconds + minutesToAdd * 60;
  60. var msToAdd = secondsToAdd * 1000;
  61. var finalDate = new Date(dateWithDays.getTime() + msToAdd);
  62. return finalDate;
  63. }