So I have a number of months and 3 date fields (for both start and end) - I have a function in progress that kinda works (from a developer/js hack, not me) which is supposed to add the number of months (term) to the start date to set the 'end date' to the correct date... but its using math to create dates instead of a true calendar so if I put in 12/31/2000 with a term of 12 months (include the start month as one so +11 mos) it returns 11/31/2001 - which doesn't exist (Nov. has only 30 days) - so I'm open to a complete rewrite - but it needs to behave properly and return a real date in the 'end date' - requirements are to run the calculation on change of any of the four fields (term, start_date_month, start_date_day, start_date_year) - if any of these four fields are empty the function should not run, if a non number is entered an alert should pop up saying "enter a valid number" and when we finally run our date addition - we should return a real date and pop it into the 3 end date fields.
Heres the current code - again - i'm open to a 'fix' or a rewrite so long as it works and covers at least what the following does:
<script type="text/javascript">
function calcTermDate () {
var numOfMonths = document.getElementById('s
chednoteTe
rm').value
;
var month = document.getElementById('s
chednoteTe
rmCommence
DateMonth'
).value;
var day = document.getElementById('s
chednoteTe
rmCommence
DateDay').
value;
var year = document.getElementById('s
chednoteTe
rmCommence
DateYear')
.value;
if (year >= 1999 && year <= 2025) {
if ((month && month <= 12) && (day && day <= 31) && year && numOfMonths) {
//Joining three date fields to create a commencement date
var newDate = new Date(month + "/" + day + "/" + year);
var d = new Date(newDate);
var day = d.getDate();
//Adding term to commencement date
d.setMonth(d.getMonth() + parseInt(numOfMonths));
if (d.getDate() < day) {
d.setDate(1);
d.setDate(d.getDate() - 1); //End of Term date
}
// Spliting into three date values
var newDay = d.getDate();
var newMonth = d.getMonth();
var newYear = d.getFullYear();
}
}
// Error check
if (isNaN(month) || isNaN(day) || isNaN(year)) {
alert ("Invalid Date");
}
// Populating End of Term date values if created
if (newDay && newMonth && newYear) {
if (document.getElementById('
schednoteT
ermCommenc
eDateMonth
')) {
if (newMonth < 10) { newMonth = '0'+newMonth}
document.getElementById('s
chednoteTe
rmEndDateM
onth').val
ue = newMonth;
document.getElementById('e
ndMonth').
value = newMonth;
}
if (document.getElementById('
schednoteT
ermCommenc
eDateDay')
) {
if (newDay < 10) { newDay = '0'+newDay}
document.getElementById('s
chednoteTe
rmEndDateD
ay').value
= newDay;
document.getElementById('e
ndDay').va
lue = newDay;
}
if (document.getElementById('
schednoteT
ermCommenc
eDateYear'
)) {
if (newYear < 10) { newYear = '0'+newYear}
document.getElementById('s
chednoteTe
rmEndDateY
ear').valu
e = newYear;
document.getElementById('e
ndYear').v
alue = newYear;
}
} else {
document.getElementById('s
chednoteTe
rmEndDateM
onth').val
ue = '';
document.getElementById('e
ndMonth').
value = '';
document.getElementById('s
chednoteTe
rmEndDateD
ay').value
= '';
document.getElementById('e
ndDay').va
lue = '';
document.getElementById('s
chednoteTe
rmEndDateY
ear').valu
e = '';
document.getElementById('e
ndYear').v
alue = '';
}
}// End of calcTermDate
</script>
<div>Term: (Months)<br><input type="text" id="schednoteTerm" name="schednoteTerm" value="" onChange="" onBlur="calcTermDate();"><
/div>
<div>Start Date:<br>
<input type="text" id="schednoteTermCommenceD
ateMonth" name="schednoteTermCommenc
eDateMonth
" maxlength="2" value="" onChange="calcTermDate();"
> /
<input type="text" id="schednoteTermCommenceD
ateDay" name="schednoteTermCommenc
eDateDay" maxlength="2" value="" onChange="calcTermDate();"
> /
<input type="text" id="schednoteTermCommenceD
ateYear" name="schednoteTermCommenc
eDateYear"
maxlength="4" value="" onChange="calcTermDate();"
>
</div>
<div>End Date:<br>
<input type="text" id="schednoteTermEndDateMo
nth" name="schednoteTermEndDate
Month" maxlength="2" value=""> /
<input type="text" id="schednoteTermEndDateDa
y" name="schednoteTermEndDate
Day" maxlength="2" value=""> /
<input type="text" id="schednoteTermEndDateYe
ar" name="schednoteTermEndDate
Year" maxlength="4" value="">
</div>
Start Free Trial