Created
December 1, 2020 16:16
-
-
Save scsskid/94d507ab55b1606afc108ada36daa8eb to your computer and use it in GitHub Desktop.
[Calculate new Date off Date] #date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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