I want to take a date value from an input field and calculate a future date (90 days from the input date).
I get inconsistent results.
if I use "1/1/11" I get "4/5/11" or 94 days answer should be "4/1/11".
if I use "3/1/11" I get "6/4/11" or 64 days where the answer should be "6/30/11"
Here is my function:
The g.process.isDat e merely allows me to enter either a date with / or - and use either 2 or 4 digit year. The returned result is always a string value "mm/dd/yyyy". This then serves as the input for the function above.
Here is that function but it seems to work just fine. I only include it to show all work used to compute the future date.
I get inconsistent results.
if I use "1/1/11" I get "4/5/11" or 94 days answer should be "4/1/11".
if I use "3/1/11" I get "6/4/11" or 64 days where the answer should be "6/30/11"
Here is my function:
Code:
g.inspDate = document.getElementById('inspDate');
g.inspDate.addEventListener("change", function(){
if (g.process.isDate(this))
{
wkDay = new Date(this.value);
nextD = wkDay.getDate() +90 ;
wkDay.setDate( nextD );
document.getElementById("followDate").value = (wkDay.getMonth()+1)+'/'+wkDay.getDay()+'/'+wkDay.getFullYear();
}
}, false);
Here is that function but it seems to work just fine. I only include it to show all work used to compute the future date.
Code:
this.isDate = function (fld) {
var mo, day, yr;
var entry = fld.value;
var reLong = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/;
var reShort = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{2}\b/;
var valid = (reLong.test(entry)) || (reShort.test(entry));
if (valid) {
var delimChar = (entry.indexOf("/") != -1) ? "/" : "-";
var delim1 = entry.indexOf(delimChar);
var delim2 = entry.lastIndexOf(delimChar);
mo = parseInt(entry.substring(0, delim1), 10);
day = parseInt(entry.substring(delim1+1, delim2), 10);
yr = parseInt(entry.substring(delim2+1), 10);
// handle two-digit year
if (yr < 100) {
var today = new Date();
// get current century floor (e.g., 2000)
var currCent = parseInt(today.getFullYear() / 100) * 100;
// two digits up to this year + 15 expands to current century
var threshold = (today.getFullYear() + 15) - currCent;
if (yr > threshold) {
yr += currCent - 100;
} else {
yr += currCent;
}
}
var testDate = new Date(yr, mo-1, day);
if (testDate.getDate() == day) {
if (testDate.getMonth() + 1 == mo) {
if (testDate.getFullYear() == yr) {
// fill field with database-friendly format
fld.value = mo + "/" + day + "/" + yr;
return true;
} else {
alert("There is a problem with the year entry.");
}
} else {
alert("There is a problem with the month entry.");
}
} else {
alert("There is a problem with the date entry.");
}
} else {
alert("Incorrect date format. Enter as mm/dd/yyyy.");
}
return false;
};
Comment