Calculating date / time to trigger an if statement
I need some help working out the following code. It is suppose to:
1. Check the Arrival and Departure date - if they are different - then it will caculate the number of days between the two dates.
2. If the Arrival and Departure dates are the same, it should compare the Arrival and departure times.
3. If the time difference is greater than 4 hours it should return a value of 1.
4. if the time difference is less than 4 hours it should return a value of 0.
The code does work for the most part, but does not return the correct value when comparing the time difference. - it returns a value of 1 whether the time difference is either greater or less than 4 hours.
// Work Area for Driver time;
// Custom calculate script
(function () {
var sStart1 = getField("ADate1").valueAsString;
var sEnd1 = getField("DDate1").valueAsString;
var atime1 = getField("ATime1").value;
var dtime1 = getField("DTime1").value;
var dStart1, dEnd1, diff1;
if(sStart1 && sEnd1) {
if(sStart1 !== sEnd1){
dStart1 = util.scand("m/d/yy", sStart1);
dEnd1 = util.scand("m/d/yy", sEnd1);
diff1 = dEnd1.getTime() - dStart1.getTime();
event.value = Math.floor(diff1 / 864e5)+1;
}
else {
event.value = "";
if (atime1 && dtime1){
function parseTime(aTime1){
if (aTime1 === '') return null;
var a1 = new Date();
var time = aTime1.match(/(\d+)(:(\d\d))?\s*(p?)/);
a1.setHours( parseInt(time[1]) + ( ( parseInt(time[1]) < 12 && time[4] ) ? 12 : 0) );
a1.setMinutes( parseInt(time[3]) || 0 );
a1.setSeconds(0, 0);
return a1;
}
if (atime1 !== "" && dtime1 !== "") {
var atime1a = parseTime(atime1);
var dtime1a = parseTime(dtime1);
var time = (dtime1a - atime1a)/(1000*60*60);
var four = (1000*60*60*4);
if (time => four){
event.value = "1";
}
else{
event.value = "0";
}
}
}}}
else {
event.value = "";
}})();
