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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sibeesh VenuRank #27 · 42.9K reputationPosted Jan 6, 2015, 6:37 PM EST
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/