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

How to fix script error(s) in computing total hours given Start and Finish Time?

Contributor ,
Sep 17, 2022 Sep 17, 2022

Copy link to clipboard

Copied

Good day!

Last week I posted a question about getting the hours difference between the Start Time and Finish Time belonging to the same day.

The solution I got seemed to be ok except when I entered the following:

Start Time = 8:00 am

Finish Time = 8:15 am

Unbilled Hours = 0.00

 

The formula for computing Total Hours = (Finish Time – Start Time) – Unbilled Hours.

 

The correct answer should be 0.25 Hours but it is giving me 0.30 hour

 

The result is the same if I entered the following:

Start Time = 8:00 am

Finish Time = 8:45 am

The correct answer should be 0.75 Hour but it is giving me 0.80

 

On the other hand, if I enter the following:

Start Time = 8:00 am

Finish Time = 8:30 am

The result is correct which is 0.50 hours.

 

The solution I got consisted of a document-level script (name “Time”), as shown below:

 

function Time(start,finish,unpaid){

if (start!="" && finish!="") {

                var h0=Number(start.substr(0,2));

                if (start.indexOf("am")>-1 && start.substr(0,2)=="12") var h0=0;

                else if (start.indexOf("pm")>-1 && start.substr(0,2)!="12") h0+=12;

                var m0=+Number(start.substr(3,2));

                var t0=h0*60+m0;

                var h1=Number(finish.substr(0,2));

                if (finish.indexOf("am")>-1 && finish.substr(0,2)=="12") var h1=0;

                else if (finish.indexOf("pm")>-1 && finish.substr(0,2)!="12") h1+=12;

                var m1=+Number(finish.substr(3,2));

                var t1=h1*60+m1;

                var delta=(t1-t0)/60;

                if (delta>0) event.value=(delta-Number(unpaid)).toFixed(1);

                else {

                                event.value="";

                                app.alert("Finish Time should not be earlier than Start Time.",3);

                }

} else {

                event.value="";

}

}

 

And the Custom Calculate Script is…

 

Time(this.getField("F02001d").value,this.getField("F02001e").value,this.getField("F02001f").value)

 

Field names are:

F02001d = Start Time

F02001e = Finish Time

F02001f = Unbilled Hours

 

I hope someone can help me resolve this issue with the computation.

 

Thank you in advance.

TOPICS
PDF forms

Views

458

Translate

Translate

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 guidelines

correct answers 2 Correct answers

Community Expert , Sep 17, 2022 Sep 17, 2022

Hi,

Correct, use "toFixed(2)".

You can't use "event.value" in the function in document-level. Use the name of the field the function comes from.

@+

Votes

Translate

Translate
Contributor , Sep 18, 2022 Sep 18, 2022

Thanks Bernd. Script is working correctly.

Votes

Translate

Translate
Community Expert ,
Sep 17, 2022 Sep 17, 2022

Copy link to clipboard

Copied

Try:

toFixed(2)

Votes

Translate

Translate

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 guidelines
Contributor ,
Sep 18, 2022 Sep 18, 2022

Copy link to clipboard

Copied

Thanks Bernd. Script is working correctly.

Votes

Translate

Translate

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 guidelines
Community Expert ,
Sep 17, 2022 Sep 17, 2022

Copy link to clipboard

Copied

Hi,

Correct, use "toFixed(2)".

You can't use "event.value" in the function in document-level. Use the name of the field the function comes from.

@+

Votes

Translate

Translate

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 guidelines
Community Expert ,
Sep 17, 2022 Sep 17, 2022

Copy link to clipboard

Copied

It is possible to use event.value in a function.

Votes

Translate

Translate

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 guidelines
Contributor ,
Sep 18, 2022 Sep 18, 2022

Copy link to clipboard

Copied

LATEST

Thanks Bebarth. Script is working correctly.

Votes

Translate

Translate

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 guidelines