Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
9

How to calculate time difference and exponential calculations

Explorer ,
Jun 13, 2023 Jun 13, 2023

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

kennethkamchuh21426993_1-1686691866469.png

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!

kennethkamchuh21426993_4-1686692950947.png

 

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 

 

TOPICS
General troubleshooting , How to , JavaScript , PDF forms
1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Jun 14, 2023 Jun 14, 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);}

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 13, 2023 Jun 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 14, 2023 Jun 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. 

kennethkamchuh21426993_1-1686776627983.png

 

 

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. 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 14, 2023 Jun 14, 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);}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 18, 2023 Jun 18, 2023

Thanks heaps! This works perfectly for me and I have combined this with the exponential function you've provided before! 

Thanks again!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 13, 2023 Jun 13, 2023

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

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 14, 2023 Jun 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

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 14, 2023 Jun 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;
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 02, 2024 Mar 02, 2024
LATEST

how to write log(a)/log(b)/log(c) in the JavaScript?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines