Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    May 2004
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question add 1 yr to existing yr

    hi ,

    there are 2 jsp fields in my jsp .

    startDate,endDate

    if startDate =12-05-2004

    endDate field should automatically display
    endDate=12-05-2005

    should add 1 yr.

    i also need to follow the same date format.

    how to do pls advise.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,033
    Thanks
    0
    Thanked 248 Times in 245 Posts
    var endDate = new Date("12-05-2004");
    endDate.setFullYear(endDate.getFullYear()+1);
    alert(endDate);

  • #3
    New Coder
    Join Date
    May 2004
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    Hi,

    when i use this function i get NAN.
    my 2 fields expDate,effDate are String datatype in my form.

    function showExpDate(form)
    {
    var expDate = new Date(form.effDate.value);
    alert(expDate);
    expDate.setFullYear(expDate.getFullYear()+1);
    form.expDate.value =expDate this should be a string value
    }

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,033
    Thanks
    0
    Thanked 248 Times in 245 Posts
    Are you sure the field effDate contains a valid date value?

  • #5
    Regular Coder
    Join Date
    Mar 2004
    Location
    Australia
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post
    One way you could do it would be to split the string up, convert the year to a number and then add 1 to it.

    something like

    Code:
    var startDate = "12-05-2004";
    var dateSplit = startDate.split("-");
    var year = Number(dateSplit[2]) + 1;
    var endDate = dateSplit[0] + "-" + dateSplit[1] + "-" + year;
    alert("Start: " + startDate + "\nEnd: " + endDate);
    you can link the variables to any form fields if needed.

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    jsp ain't that java?

  • #7
    Regular Coder
    Join Date
    Mar 2004
    Location
    Australia
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post
    my bad, didnt read the jsp part. just assumed javascript >.< maybe someone else can use it

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,033
    Thanks
    0
    Thanked 248 Times in 245 Posts
    Quote Originally Posted by Serex
    One way you could do it would be to split the string up, convert the year to a number and then add 1 to it.

    something like

    Code:
    var startDate = "12-05-2004";
    var dateSplit = startDate.split("-");
    var year = Number(dateSplit[2]) + 1;
    var endDate = dateSplit[0] + "-" + dateSplit[1] + "-" + year;
    alert("Start: " + startDate + "\nEnd: " + endDate);
    you can link the variables to any form fields if needed.
    That implementation is wrong if the start date is 2-29-2004, the end date will become 2-29-2005. Mine is correct.

    Regarding the jsp-javascript confusion, I think that this is still a javascript question because of the way hema's second post was stated:
    Quote Originally Posted by hema
    Hi,

    when i use this function i get NAN.
    my 2 fields expDate,effDate are String datatype in my form.

    function showExpDate(form)
    {
    var expDate = new Date(form.effDate.value);
    alert(expDate);
    expDate.setFullYear(expDate.getFullYear()+1);
    form.expDate.value =expDate this should be a string value
    }
    You will get a NaN date if the date string passed is not a valid date. That's why I asked hema earlier about the validity of the value of the effDate field.
    If effDate is a user input date, then the code should look like this:
    Code:
    function showExpDate(form)
    {
       alert(form.effDate.value);
       var expDate = new Date(form.effDate.value);
       alert(expDate);
       if (!isNaN(expDate)) {
          expDate.setFullYear(expDate.getFullYear()+1);
          form.expDate.value = expDate.getMonth()+1 + "-" + expDate.getDate() + "-" + expDate.getFullYear();
       }
       else {
          alert("Invalid date!");
          form.effDate.focus();
       }
    }

  • #9
    Regular Coder
    Join Date
    Mar 2004
    Location
    Australia
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post
    ta glen. i learn something new every day. Im still sure mine would have done the job

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,033
    Thanks
    0
    Thanked 248 Times in 245 Posts
    Quote Originally Posted by Serex
    Im still sure mine would have done the job
    Is 2-29-2005 a valid date (leap year)?
    Quote Originally Posted by glenngv
    That implementation is wrong if the start date is 2-29-2004, the end date will become 2-29-2005.

  • #11
    Regular Coder
    Join Date
    Mar 2004
    Location
    Australia
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post
    aahh i get it now ta


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •