Skip to main content
first.officer
Inspiring
December 5, 2021
Question

Display field based on time field comparison

  • December 5, 2021
  • 1 reply
  • 263 views

Hi all,

 

I have the following script that either nakes a field 'visible' or 'hidden', depending on the compariosn of two other fields;

 

var a = this.getField("Leg1time").value;
var b = this.getField("Leg2time").value;

 

if (b>a)
{event.target.display = display.visible;
}
else if (b<=a)
{event.target.display = display.hidden;
}

 

The question is.....if instead I want the field to be visible in the case of 'b' being greater than 'a' (+ 3 minutes) , how to achieve this?. 

 

The following code works to convert to a numeric value, and is easy enough to resolve then with normal validations - the issue is that when entering a time value in the 'Leg2time' that is earlier than the 'Leg1time', the value is not a negative value, which can be easily resolved using a validation, as the number calculated is greater than the +3 minutes also (e.g. if 'Leg1time' value is 09:59, and 'Leg2time' is 09:58, then a value of 1439 is calculate, and i can see this is beacuse it is converting a numerical value from essentially a 23h59m period elapsing between them.

 

function parseTime(cTime)
{
if (cTime == '') return null;
var d = new Date();
var time = cTime.match(/(\d+)(:(\d\d))?\s*(p?)/);
d.setHours( parseInt(time[1]) + ( ( parseInt(time[1]) < 12 && time[4] ) ? 12 : 0) );
d.setMinutes( parseInt(time[3]) || 0 );
d.setSeconds(0, 0);
return d;
}

var cStart = this.getField("Leg1time").value;
var cStop = this.getField("Leg2time").value;

if (cStart != "" && cStop != "") {
var tStart = parseTime(cStart);
var tStop = parseTime(cStop);
if (tStop<tStart) tStop.setTime(tStop.getTime()+24*60*60*1000);
var diff = (tStop - tStart)/(1000*60);
event.value = diff;
}
else {
event.value = "";
}

 

Any assistance greatly appreciated 😉

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
December 5, 2021

Assuming the parseTime function works correctly (which I didn't verify) and returns the time in minutes, you can use this code:

 

var a = parseTime(this.getField("Leg1time").valueAsString);
var b = parseTime(this.getField("Leg2time").valueAsString);

if (b>(a+3)) {
	event.target.display = display.visible;
} else {
	event.target.display = display.hidden;
}