Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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);}
Copy link to clipboard
Copied
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 = "";}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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);}
Copy link to clipboard
Copied
Thanks heaps! This works perfectly for me and I have combined this with the exponential function you've provided before!
Thanks again!
Copy link to clipboard
Copied
It should be Math.log() not math.log().
If you use a negative number inside Math.log() you will get a NaN error.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
how to write log(a)/log(b)/log(c) in the JavaScript?
Find more inspiration, events, and resources on the new Adobe Community
Explore Now