These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

If Statement

TechDude10
Registered: Jul 1 2010
Posts: 10

I've seen examples posted before of IF statements in Javascript before, but I've tried every combination and I can't get a simple IF statement to work.

I have a fillable PDF which deals with time employees work.

There's Start_Time1, Lunch1, End_Time1, and Total_Hours1 fields
I'm converting the time entered to "Military time" in order to subtract hours and a final hour count.
Without an IF statement, this bit of code gives me the correct answer:

((End_Time1 + 12) - Start_Time1) - Lunch1

Example, End_Time1 = 5 - (5pm)
Start_Time1 = 8 - (8am)
Lunch1 = 1 - (1 hour)
The code will give me a total hours of 8 from the 17-8-1.

However, some part-time people come in at 1pm or (13:00) and the above code won't work properly.

Below is the code I need, I've removed all attempts of Javascripting since none work.

if (Start_Time1 > 7) then
((End_Time1 + 12) - Start_Time1) - Lunch1
else
if (Start_Time1 < 7) {((End_Time1 + 12) - (Start_Time1 + 12)) - Lunch1

Any help is appreciated. I'm a VB pro, not so much a Java :)

My Product Information:
Acrobat Standard 9.3.1, Windows
try67
Expert
Registered: Oct 30 2008
Posts: 2398
I'm assuming all of these fields are just numbers, not Date objects. If this is not correct, then the code below will not work.
Start_Time1 = this.getField("Start_Time1").value;End_Time1 = this.getField("End_Time1").value;Lunch1 = this.getField("Lunch1").value; if (Start_Time1>7) {event.value = ((End_Time1 + 12) - Start_Time1) - Lunch1;} else if (Start_Time1 < 7) {event.value = ((End_Time1 + 12) - (Start_Time1 + 12)) - Lunch1}

PS - You should also probably handle the case when Start_Time1 is exactly 7.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
First you can not use the 'Simplified Field Notation' is very limited to most simple field names and some basic operations. You can not access any field properties, methods, call functions, or use any logical control statements, 'if'.

You will have to write this as a 'Custom JavaScript calculation' in JavaScript. JavaScript is an object orientated language, so you must obtain the object you are interested in before you can access any property of the field, value, or method.

The date and time values in a field are specially formatted character strings. So you are going to need to convert these strings into a date or time object.
Looked at [url=http://www.acrobatusers.com/tutorials/2006/date_time_part1]Working with date and time in Acrobat JavaScript (Part 1 of 3)[/url] by Thom Parker to see what is involved.

See [url=https://acrobat.com/#d=7mFLg0SJD18SwNzcEj9*sw]Weekly Time Sheet[/url] for a working example. Since there are a number of repeated calculaitons, functions have been used for reuse and ease of maintaining the code, code in only one location needs to be updated for modifications.

I would by figuring out how to compute a simple time difference in time using a start and end time.

You should have access the [url=https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference]Mozilla JS 1.5 Reference[/url] and the [url=http://www.adobe.com/devnet/acrobat/javascript.php]Acrobat JS API Reference[/url].

George Kaiser

TechDude10
Registered: Jul 1 2010
Posts: 10
Thank you both for your help!! Try67, that code works perfectly, and I did forget to include the >= for 7. Thanks! And thanks gkaiseril for the other references and explaining the "Simplified" and "Custom". I always threw my code in each to see if it worked, but yeah, no go. You both saved me a lot of time!! Thanks again!!!
TechDude10
Registered: Jul 1 2010
Posts: 10
I tried looking at the Timesheet link you gave me, but the form i previewed and downloaded has no cells to fill in. No calculations or anything, just a simple print and write in form. Maybe I'm missing something... And, the page 3 of that link about date/time is also "not found". I did run into the time issue surrounding 15 minutes, 30, or 45 minutes past an hour. That screwed up the math. Unfortunately not everyone works on even hours. I'll look around more about time calculations beyond even hours. Thanks!
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
The updated [url=https://acrobat.com/#d=xVmbN3cQ*x2Prm9in4wrnw]Timesheet[/url].

George Kaiser

TechDude10
Registered: Jul 1 2010
Posts: 10
Thanks for the update, I've read a lot about the document level javascript, but can't seem to create it, or find it in this case. Our time sheet that I was tasked to make fillable for distribution, simply has a start, lunch length (usually 1 hour), end work, and a total at the end. If I could modify the form from the link, that'd be great, but can't open the document level java to edit the fields it's pulling data from. I have Acrobat 9 Standard, and I've yet to find any forum that explains how to create the document level java.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
To edit the document level scripts, you need the Professional version. But it should be possible to add function scripts to the open page action and have them initialize when the page opens. For a one page form there is little chance of the code running twice, but one could add a variable to prevent that.

George Kaiser