• Global community
• Language:
• Deutsch
• English
• Español
• Français
• Português
• 日本語コミュニティ
Dedicated community for Japanese speakers
• 한국 커뮤니티
Dedicated community for Korean speakers
Exit
• How to calculate time difference and exponential c...

# How to calculate time difference and exponential calculations

Explorer ,
Jun 13, 2023 Jun 13, 2023

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

TOPICS
General troubleshooting , How to , JavaScript , PDF forms

Views

583

Report

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

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 =``````
...

8 Replies 8
Community Expert ,
Jun 13, 2023 Jun 13, 2023

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 = "";}

Report

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

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.

Report

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

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);}``````

Report

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

Copied

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

Thanks again!

Report

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

Copied

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

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

Report

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

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

Report

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

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;``````

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Mar 02, 2024 Mar 02, 2024

Copied

LATEST

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