I'm a novice to JavaScript but wanted to know how I could get a "Expire Date" box to automatically give me a date 1 year from the "Date Form Completed" box in a form I'm working on. I have no starting code, just the two boxes.
thanks
Anthony
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sibeesh VenuPosted Jan 6, 2015, 11:37 PM
JavaScript contains a native Date object, you can add months to it as follows...
var endDate = document.getElementById('enddate').value;
var date = new Date(endDate); date.setMonth(date.getMonth() + 2);
// Yes it will calculate going over a year etc
prevExpiryDate = date.getFullYear() + "/" + (date.getMonth() + 1) + "/" +
// Months are 0 for January etc.
date.getDate(); Note
- The format you are inputting the value to the Date object is important you will want to validate this first.
- When we are creating the expiry date the value will not be prepended with zeros 2012/12/09, it will be 2012/12/9
Or you can use a library as suggestedRead more here
http://stackoverflow.com/questions/12798535/javascript-display-expiry-date-upon-user-input-of-start-end-date
Please accept and up vote if it helped you
Kindest Regards
Sibeesh
http://sibeeshpassion.com/