...

View Full Version : add 1 yr to existing yr



hema
06-01-2004, 09:00 AM
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.

glenngv
06-01-2004, 11:39 AM
var endDate = new Date("12-05-2004");
endDate.setFullYear(endDate.getFullYear()+1);
alert(endDate);

hema
06-01-2004, 12:21 PM
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
}

glenngv
06-01-2004, 12:47 PM
Are you sure the field effDate contains a valid date value?

Serex
06-01-2004, 06:41 PM
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



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.

Garadon
06-01-2004, 07:19 PM
jsp ain't that java?

Serex
06-01-2004, 10:23 PM
my bad, didnt read the jsp part. just assumed javascript >.< maybe someone else can use it :p

glenngv
06-02-2004, 06:53 AM
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



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. :p :)

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


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:


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();
}
}

Serex
06-02-2004, 10:34 AM
ta glen. i learn something new every day. Im still sure mine would have done the job :p

glenngv
06-02-2004, 11:57 AM
Im still sure mine would have done the job :p
Is 2-29-2005 a valid date (leap year)?


That implementation is wrong if the start date is 2-29-2004, the end date will become 2-29-2005.

Serex
06-02-2004, 03:00 PM
aahh i get it now :p ta



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum