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

Date/Time calculations

rpeterson
Registered: Oct 7 2008
Posts: 72
Answered

Hi all,
I am trying to set up a weekly work schedule for a coworker that will calculate the number of hours worked per day. Here is what I have so far:

----- form1.#subform[0].NumericField1::calculate: - (FormCalc, client) -----------------------------

((Time2Num("form1.#subform.DateTimeField2.formattedValue")) - (Time2Num("form1.#subform.DateTimeField1.formattedValue")))/((1000*60*60)/24)

I cannot seem to get the calculation to work. Can anyone tell me what I am doing wrong and provide suggestions on how to fix it and make it work? Any suggestion is welcome, even if you tell me I am expecting something from out in left field. Thank you in advance.

rpeterson

rpeterson

My Product Information:
LiveCycle Designer, Windows
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Are you getting any error messages?

Using the quotation marks around the field reference covert the item from an object to a character string.

If you are going to use the formatted value, you need to provide the picture value of the formatted string.

You only need to divide the difference by the milliseconds in an hour not milliseocnds in a day.

// ocmpute tne difference in hours and decimals of an hour between 2 times(Time2Num(form1.#subform.DateTimeField2.formattedValue, "HH:MM") - Time2Num(form1.#subform.DateTimeField1.formattedValue, "HH:MM")) / (1000 * 60 * 60)

George Kaiser

rpeterson
Registered: Oct 7 2008
Posts: 72
gkaiseril,

I removed the quotes, pared the parentheses down to match your example... I am not getting, nor have I gotten, any error messages. the numeric field shows 0.00 the entire time. if I take away the numeric formatting, it shows 0 the entire time.

I have the calculation in the script for the numericfield. should it be somewhere else? i am currently using a blank document until I get the calculations working. after that i plan on incorporating them into a form wrapped into a subform. should it be dynamic? i am at a loss.

rpeterson

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You need to provide the formatting string for the time fields. In my example, I use 24 hour or military time. You should also complete all of the fields for formatting, especially the vailtion field because if the time entered is not in the exact format expected, the Time2Num function will return 0.

George Kaiser

rpeterson
Registered: Oct 7 2008
Posts: 72
gkaiseril,

where do i put the formatting string? are you referring to the HH:MM in the Time2Num? i am using formcalc. is it in the object/field/display pattern palette? i have tried both the HH:MM and HH:MM A for the DateTimeFields.

rpeterson

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You have to establish the format for the field. To get the 'Time2Num()' function to work you need to use the same formatting string for the second parameter of the function.

You can use the following code to help see what is going on.
// exit scirpt for DateTimeField1xfa.host.messageBox(Concat("Entered value for DateTimeField1: ", DateTimeField1.formattedValue) );

// exit scirpt for DateTimeField2xfa.host.messageBox(Concat("Entered value for DateTimeField12: ", DateTimeField2.formattedValue) );

// calculation for the difference fieldif(HasValue(form1.#subform.DateTimeField2) & HasValue(form1.#subform.DateTimeField2) ) then// ocmpute tne difference in hours and decimals of an hour between 2 timesxfa.host.messageBox(Concat("Time2Num(DateTimeField1.formattedValue, 'HH.MM') ", Time2Num(form1.#subform.DateTimeField1.formattedValue, "HH:MM")))xfa.host.messageBox(Concat("Time2Num(DateTimeField2.formattedValue, 'HH.MM') ", Time2Num(form1.#subform.DateTimeField2.formattedValue, "HH:MM"))) (Time2Num(form1.#subform.DateTimeField2.formattedValue, "HH:MM") - Time2Num(form1.#subform.DateTimeField1.formattedValue, "HH:MM")) / (1000 * 60 * 60)elsenullendif

George Kaiser

rpeterson
Registered: Oct 7 2008
Posts: 72
gkaiseril,

as far as i can tell the second parameter of the Time2Num function for each DateTimeField is identical. can you be more explicit? here is what i have currently:

----- form1.#subform[0].NumericField1::calculate: - (FormCalc, both) -------------------------------

(Time2Num(form1.#subform.DateTimeField2.formattedValue, "h:MM A") - Time2Num(form1.#subform.DateTimeField1.formattedValue, "h:MM A")) / (1000*60*60)

rpeterson

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
The format for the field being used in a function must match the format string used in the furnciton.

You need to make each time field have the following:

Field tab Dispaly pattern: h:MM A
Field tab Edit pattern: h:MM A

Value tab Validation pattern: h:MM A

Binding tab Date pattern: h:MM A
Bindng tab Data Format: Time

You have to enter 9:00 AM for 9 o'clock in the morning and 12:30 PM for 12:30 in the afternoon. I then get 2.5 for an answer. But there is garbage until I have both times entered.

George Kaiser

rpeterson
Registered: Oct 7 2008
Posts: 72
gkaiseril,

Thank you so much for your help. I did not see the lower part of your next to the last post, I am sorry for that. I truly appreciate your time and patience. Until next time...

rpeterson

rpeterson

mich_trab
Registered: May 5 2009
Posts: 2
Hello,

I have to calculate two time fields to get a total time worked. Displayed in military time.

Can someone help?
thanks,
Mich
rpeterson
Registered: Oct 7 2008
Posts: 72
I just learned this myself from gkaiseril. here is my code with some generics used instead of my field names...
((Time2Num(form1.#subform.TimeOut.formattedValue, "HH:MM") - Time2Num(form1.#subform.TimeIn.formattedValue, "HH:MM")) / (1000*60*60)

Hope this helps.
rpeterson

rpeterson

mich_trab
Registered: May 5 2009
Posts: 2
Thanks rpeterson,
But, I'm so new at this I'm not sure where to put the code?
My time field names are timeIn, timeOut, timeTotal and I'm guessing I'll enter your code in the
dialog box: Text Field Properties under the Calculate tab-Custom calculation script?
I appreciate any help,
mich
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You might want to look at some of trainging material posted to learn how to make a baic form and add some calculatiions or other actions to a form.

There are also 2 different programs that create forms, Acrobat and LiveCycle Designer, but each has a unique syntax for its scripting language. From your discription, it sounds like you are using Acrobat and the script is very different from the provided script which is for LiveCycle Designer.

George Kaiser

FlyGirl
Registered: Apr 2 2009
Posts: 10
I am still having trouble with this. I tried all formulas that I can think of and other formulas provided here and other forums (adjusting accordingly for my field names of course). No break throughs yet. This is the only thing I have left to do for this form and it has taken me weeks to figure out to no avail. All I want is the time difference displayed but I'm learning as I go with this program. Unfortunatley, I don' t understand a lot of the scripting stuff which is probably why I haven't been able to figure this out. Any help towards the solution is great appreciated.

Here's what I have (I used the formula from this post):

In a table, I have a DutyOn field. I have it set as a Date/Time Field. with the display patterns as HH:MM. In the exit script using JavaScript:

// exit scirpt for DutyOn
xfa.host.messageBox(Concat("Entered value for DutyOn: ", DutyOn.formattedValue) );

Next to DutyOn is a DutyOff Field with similar info. Date/Time Field, display pattern as HH:MM. In the exit script using JavaScript:

// exit scirpt for DutyOff
xfa.host.messageBox(Concat("Entered value for DutyOff: ", DutyOff.formattedValue) );



Next is the TtlDuty field. It again is a Date / Time field (also tried it as a Numeric Field). As a calculation using JavaScript:

// calculation for the difference field
if(HasValue(form1.#subform.DutyOff) & HasValue(form1.#subform.DutyOff) ) then
// ocmpute tne difference in hours and decimals of an hour between 2 times
xfa.host.messageBox(Concat("Time2Num(DutyOn.formattedValue, 'HH.MM') ", Time2Num(form1.#subform.DutyOn.formattedValue, "HH:MM")))
xfa.host.messageBox(Concat("Time2Num(DutyOff.formattedValue, 'HH.MM') ", Time2Num(form1.#subform.DutyOff.formattedValue, "HH:MM")))

(Time2Num(form1.#subform.DutyOff.formattedValue, "HH:MM") - Time2Num(form1.#subform.DutyOn.formattedValue, "HH:MM")) / (1000 * 60 * 60)
else
null
endif



Idea? Suggestions? It's like my form doesn't want to read Javascript or something.

Thanks in advance :)
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Why are you placing a decimal number into field formatted for a date/time format?

Try the following code for the calculation script to see what is going on:
// calculation for the difference fieldif(HasValue(DutyOff) & HasValue(DutyOn) ) then// display DutyOn input and value for computationxfa.host.messageBox(Concat(DutyOn.formattedValue, " Time2Num(DutyOn.formattedValue, 'HH.MM'): ", Time2Num(DutyOn.formattedValue, "HH:MM")), "DutyOn Values", 3, 0)// display DutyOff input and value for computationxfa.host.messageBox(Concat(DutyOff.formattedValue, " Time2Num(DutyOff.formattedValue, 'HH.MM'): ", Time2Num(DutyOff.formattedValue, "HH:MM")), "DutyOff Values", 3, 0)// cmpute tne difference in hours and decimals of an hour between 2 timesvar DutyTime =(Time2Num(DutyOff.formattedValue, "HH:MM") - Time2Num(DutyOn.formattedValue, "HH:MM")) / (1000 * 60 * 60)// display result of calculationxfa.host.messageBox(Concat("DutyTime =  ", DutyTime), "Elapsed Time Reslut", 3, 0)// set decimal field valueDutyTimeelsenullendif

If your times do not occur on the same date, your script wiil not work properly.

George Kaiser

cspear
Registered: Dec 2 2010
Posts: 1
will this syntax work in Adobe Pro and for military to civilian calculations? I have a client who is inputing military time and trying to calculate and getting 0.

thanks
adobeone2010
Registered: Nov 23 2010
Posts: 27
I had the same problem and it was solved. See my post

http://acrobatusers.com/forum/forms-livecycle-designer/calculating-time-please-help