12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- import addDays from "../addDays/index.js";
- import addMonths from "../addMonths/index.js";
- import toDate from "../toDate/index.js";
- import requiredArgs from "../_lib/requiredArgs/index.js";
- import toInteger from "../_lib/toInteger/index.js";
- export default function add(dirtyDate, duration) {
- requiredArgs(2, arguments);
- if (!duration || typeof duration !== 'object') return new Date(NaN);
- var years = 'years' in duration ? toInteger(duration.years) : 0;
- var months = 'months' in duration ? toInteger(duration.months) : 0;
- var weeks = 'weeks' in duration ? toInteger(duration.weeks) : 0;
- var days = 'days' in duration ? toInteger(duration.days) : 0;
- var hours = 'hours' in duration ? toInteger(duration.hours) : 0;
- var minutes = 'minutes' in duration ? toInteger(duration.minutes) : 0;
- var seconds = 'seconds' in duration ? toInteger(duration.seconds) : 0;
- var date = toDate(dirtyDate);
- var dateWithMonths = months || years ? addMonths(date, months + years * 12) : date;
- var dateWithDays = days || weeks ? addDays(dateWithMonths, days + weeks * 7) : dateWithMonths;
- var minutesToAdd = minutes + hours * 60;
- var secondsToAdd = seconds + minutesToAdd * 60;
- var msToAdd = secondsToAdd * 1000;
- var finalDate = new Date(dateWithDays.getTime() + msToAdd);
- return finalDate;
- }
|