• Global community
• Language:
• Deutsch
• English
• Español
• Français
• Português
• 日本語コミュニティ
Dedicated community for Japanese speakers
• 한국 커뮤니티
Dedicated community for Korean speakers
• How to fix script error(s) in computing total hour...

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

Participant ,
Sep 17, 2022 Sep 17, 2022

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.

TOPICS
PDF forms

Views

67

Likes

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 , 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.

@+

Likes

Participant , Sep 18, 2022 Sep 18, 2022

Thanks Bernd. Script is working correctly.

Likes

5 Replies 5
Community Expert ,
Sep 17, 2022 Sep 17, 2022

Copied

Try:

toFixed(2)

Likes

Report

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

Copied

Thanks Bernd. Script is working correctly.

Likes

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

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.

@+

Likes

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

Copied

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

Likes

Report

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

Copied

LATEST

Thanks Bebarth. Script is working correctly.

Likes

Report

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