Skip to main content
kenneth kam chuh21426993
Known Participant
June 13, 2023
Answered

How to calculate time difference and exponential calculations

  • June 13, 2023
  • 2 replies
  • 1614 views

I have two time fields in my form that looks like this: 

I want product time 2 minus product time 1 to give me the time difference in minutes. In this case, it's 2 minutes. May I know what's the script for this?

 

I want to use the calculated time difference (i.e. 2 min) to put into the formula below. Can anyone assist me with the scripts? Thanks!

 

Where t = 2 min and e is the base of natural logarithm 

Presumably this would be something like: 

 

var t = getField("time diff").value; 
event.value = 600/(math.log(-0.010193*t)) 

 

I hope this makes sense 

 

This topic has been closed for replies.
Correct answer Nesa Nurani

You can use this:

function minutesDiff(dateTimeValue2, dateTimeValue1) {
var differenceValue =(dateTimeValue2.getTime() - dateTimeValue1.getTime()) / 1000;
differenceValue /= 60;
return Math.abs(Math.round(differenceValue));}

var dd = this.getField("ProductCal").valueAsString;
var rd = this.getField("sampling").valueAsString;
if(dd === "" || rd === "")
event.value = "";
else{
dateTimeValue1 = util.scand("dd/mm/yyyy HH:MM", dd);
dateTimeValue2 = util.scand("dd/mm/yyyy HH:MM", rd);

event.value = minutesDiff(dateTimeValue1, dateTimeValue2);}

2 replies

Nesa Nurani
Community Expert
Community Expert
June 14, 2023

It should be Math.log() not math.log().

If you use a negative number inside Math.log() you will get a NaN error.

kenneth kam chuh21426993
Known Participant
June 14, 2023

My apologies, I didn't mean to be natural logarithm. 

It was meant to be exponential or Euler's number - the e in the equation above, which is approximated to be 2.7183

 

Nesa Nurani
Community Expert
Community Expert
June 14, 2023

Once you obtain minutes with script posted above, you can use this:

var t = Number(this.getField("time diff").valueAsString);
if(t !== 0)
event.value = 600/(Math.exp(-0.010193*t));
else
event.value = 0;
JR Boulay
Community Expert
Community Expert
June 13, 2023

You can use this script as a Calculation script in the result field, after adapting field names and formats.

 

// calculate the difference between two dates entered in two different fields

var dd = this.getField("Check In Date");
var rd = this.getField("Check Out Date");
if (dd.value != "" && rd.value != "") {
var d1 = util.scand("mm/dd/yyyy", dd);
var d2 = util.scand("mm/dd/yyyy", rd);
var diff = (d2.valueOf() - d1.valueOf()) / 1000;
event.value = Math.round((diff / 60 / 60) / 24)+1;
}
else {event.value = "";}

Acrobate du PDF, InDesigner et Photoshopographe
kenneth kam chuh21426993
Known Participant
June 14, 2023

Thanks for this.

However, I am still stuck. 

Below is the code I have used - I am expecting the result to be 4 min - see below image. Top field "timediff" is where I put my script and expecting the answer 4  because 08:00 - 07:56 os 4 min. 

 

 

but nothing appears in my result field after putting in the calculation script

var dd = this.getField("ProductCal");
var rd = this.getField("sampling");
if (dd.value != "" && rd.value != "") {
var d1 = util.scand("dd/mm/yyyy", dd);
var d2 = util.scand("dd/mm/yyyy", rd);
var diff = (d2.valueOf() - d1.valueOf()) / 1000;
event.value = Math.round((diff / 60 / 60) / 24)+1;
}
else {event.value = "";}

Would you be able to assist me with this please.

Thanks in advance. 

 

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
June 15, 2023

You can use this:

function minutesDiff(dateTimeValue2, dateTimeValue1) {
var differenceValue =(dateTimeValue2.getTime() - dateTimeValue1.getTime()) / 1000;
differenceValue /= 60;
return Math.abs(Math.round(differenceValue));}

var dd = this.getField("ProductCal").valueAsString;
var rd = this.getField("sampling").valueAsString;
if(dd === "" || rd === "")
event.value = "";
else{
dateTimeValue1 = util.scand("dd/mm/yyyy HH:MM", dd);
dateTimeValue2 = util.scand("dd/mm/yyyy HH:MM", rd);

event.value = minutesDiff(dateTimeValue1, dateTimeValue2);}