Skip to content

Instantly share code, notes, and snippets.

@scsskid
Created December 1, 2020 16:16
Show Gist options
  • Save scsskid/94d507ab55b1606afc108ada36daa8eb to your computer and use it in GitHub Desktop.
Save scsskid/94d507ab55b1606afc108ada36daa8eb to your computer and use it in GitHub Desktop.
[Calculate new Date off Date] #date
function deltaDate(input, days, months, years) {
return new Date(
input.getFullYear() + years,
input.getMonth() + months,
Math.min(
input.getDate() + days,
new Date(
input.getFullYear() + years,
input.getMonth() + months + 1,
0
).getDate()
)
);
}
function deltaDateMomentJs(input, amount, dateType) {
const dt = new Date(input);
switch (dateType) {
case 'days':
return dt.setDate(dt.getDate() + amount) && dt;
case 'weeks':
return dt.setDate(dt.getDate() + 7 * amount) && dt;
case 'months':
return dt.setMonth(dt.getMonth() + amount) && dt;
case 'years':
return dt.setFullYear(dt.getFullYear() + amount) && dt;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment