I am trying to use javascript to calculate the difference between two form inputted dates and return the result to another form object. When I load up the page with the function on my web browser the form does not load and I get a message to say that the page contains errors, presumably as the function is not initialising or being referenced properly. Can anyone help, is the function scripted correctly and are the variables I am attempting to pass to the function referenced correctly?
I've manipulated a javascript function I found on the web until I arrived at the following:
function days_between(inStart, inEnd, outField) {
// Read values from the relevant form objects for start and end dates, and
// reference the output form object.
var sDate=document.getElementById(inStart).value;
var eDate=document.getElementById(inEnd).value;
var tmpField=document.getElementById(outField);
if (sDate != "" && eDate != "")
{// The number of milliseconds in one day
var ONE_DAY = 1000 * 60 * 60 * 24
// Convert both dates to milliseconds
var sDate_ms = sDate.getTime()
var eDate_ms = eDate.getTime()
// Calculate the difference in milliseconds
var difference_ms = Math.abs(eDate_ms - sDate_ms)
// Convert back to days and return
tmpField[0].value = Math.round(difference_ms/ONE_DAY)}
else
{tmpField[0].value = 0};
}
I am attempting to read the values in form objects 'inStart' and 'endStart', then if these values are not "" (empty) I am attempting to calculate the difference in whole days and return the result to form object 'outField' (denoted by 'tmpField' in the function).
Also, I call the function within a Flash form, within a coldfusion <cfinput> tag via the tag parameter onChange="days_between(incAbsBegan, incAbsEnded, incAbsShifts)", where 'incAbsBegan', 'incAbsEnded', 'incAbsShifts' are the names of the form objects for start date, end date and number of whole days between respectively.