Using JavaScript, I need to calculate a date in the future (two years minus one day) based on the date a user enters into a form. Any suggestions?
3 Answers
You can do this without any libraries using the Date object:
var d = new Date(inputValue);
d.setYear(d.getFullYear() + 2);
d.setDate(d.getDate() - 1);
alert(d.toString()); // Thu Jan 28 2016 <timestamp/timezone>
The inputValue would be the date entered, but it would need to be an accepted format, such as MM-DD-YYYY.
You will need d.getFullYear on the second line, since .getYear would return 114 right now (years since 1900).