1

I have a StartDate and an ExpiryDate textbox. Both take values in the forms of 10/12/2013.

What I would like to be able to do is, when you change the StartDate textbox (whether from empty or just updating the date) the ExpiryDate textbox needs to add 1 year onto the date.

Example:

If StartDate = 10/12/2013 then ExpiryDate will automatically change to 10/12/2014.

How to do that with JS?

function MyFunc() {
            MyTextBox = document.getElementById("<%= TextBox1.ClientID %>");
            MyTextBox2 = document.getElementById("<%= TextBox2.ClientID %>");
            var date = new Date(MyTextBox.value);
            var day = date.getDate();
            var month = date.getMonth() + 1;
            var year = date.getFullYear() + 1;
            MyTextBox2.value = day + "/" + month + "/" + year;
        }
2
  • 1
    var date = new Date("input text"); date.setFullYear(date.getFullYear() + 1); Commented Jul 12, 2013 at 16:31
  • updated the code above with something that seems to work fine. Just had an issue with the month, being one less since it goes from 0 to 11 instead of 1 to 12. Commented Jul 12, 2013 at 16:50

2 Answers 2

3

Try this, call the setExpiryDate() function whenever you need to set the expiration date.

function setExpiryDate() {
    var txtStartDate = document.getElementById("ctrl1");
    var txtExpiryDate = document.getElementById("ctrl2");

    var dt = new Date(txtStartDate.value);

    if (!isNaN(dt)) {
        dt = dt.setYear(dt.getYear() + 1);
        txtExpiryDate.value = padStr(temp.getDate()) + '/' + padStr(temp.getMonth() + 1) + '/' + temp.getFullYear().toString();
    }
}

function padStr(i) {
    return (i < 10) ? "0" + i : "" + i;
}
Sign up to request clarification or add additional context in comments.

Comments

0

How about this:

function updateInput(value){
    document.getElementsById('Yourelement').Value = value;
}

Other than that, all you need is some date parsing/string manipulation to find the correct year.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.