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

Calculating Time Please Help

adobeone2010
Registered: Nov 23 2010
Posts: 27
Answered

First I'm a novice so I'm not familiar with all the terms so please forgive me and use simple terms....sorry and thanks for the understanding.
 
I have LiveCycle Designer 8.0
 
I use to use excel and had a simple time in / time out time sheet, now my company switched to Adobe LiveCycle and I'm tring to put this together.
 
I've searched all the posts for days but kept getting confused and worst, none of them seem to work for me. Can someone write the script for me or explain it.
 
Here is what I have on my time sheet, I need a script to give me the times where it calculates itself, we use military time.
 
Time actually worked...Time In: 2100 / Time out: 1030 / Total hours = 13:30
 
Time scheduled to work...Time In: 2100 / Time Out: 0600 / Total hours = 9:00
 
Overtime worked = 4:30

My Product Information:
LiveCycle Designer, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
What values are you getting for your values?

Does your script work for any values?
If so, what values?

What happens when you use a start time of 21:00 and enter various end times like 23:00, 23:30 23:59, 00:00, 00:30?
Why do you think the elapsed time goes negative after 23:59?
Could it be that the end time is assumed to be the time on the same day as the start time and not the next day as you are assuming?

Time is not just a value between 0:00 (12:00 AM( and 24:00 (12:00 AM the next day) it is also the date for that time. So elapsed time is not just the difference between start time and end time but the difference of the end time and date and the start time and date. There is also time difference before and after an event for a given data and time so a negative elapsed time is a reasonable value under some situations.

LiveCycle Designer's documentation for the date and time functions refers to the number of days elapsed since the Epoch date (day conversions functions) and milliseconds since the Epoch date (time functions).

What is the Epoch date used by LiveCycle Designer?

It appears you will need to consider the actual date of the start and end dates along with the start and end time.

Many of the examples are asking for computing the elapsed time within a single date, so one can ignore the date except on the days that Day Light Savings Time starts or ends.

George Kaiser

Masi
Registered: Sep 18 2008
Posts: 22
I tested this (javascript in calculate event) that seemed to have weird bug when using times that had 08 or 09 (0800 or 0900). Can anybody explain, why parseInt("08") and parseInt("09") returns zero?

Anyways, idea is to parse given times in minutes and then calculate how many minutes the personnel was in work.

// replace TextField1 and TextField2 references to point into correct fields
var vTime1 = TextField1.rawValue;
var vTime2 = TextField2.rawValue;
var vTime1Minutes = parseInt(vTime1.substring(0, vTime1.length-2))*60 + parseInt(vTime1.substring(vTime1.length-2, vTime1.length));
var vTime2Minutes = parseInt(vTime2.substring(0, vTime2.length-2))*60 + parseInt(vTime2.substring(vTime2.length-2, vTime2.length));
if (vTime1Minutes > vTime2Minutes)
this.rawValue = (vTime2Minutes+1440 - vTime1Minutes)/60;
else
this.rawValue = (vTime2Minutes - vTime1Minutes)/60;
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Masi,

What does this have to do with the subject and problem of the OP?

George Kaiser

adobeone2010
Registered: Nov 23 2010
Posts: 27
George Kaiser,

Thank you for responding so quickly. I never considered the date into factoring in my times. Then again, how could I since this all seems foreign to me. I did a little research regarding the "Epoch Date" and now understanding its purpose doesn't change more or less my understanding of what's going on here. Bottom line, I've read hundreds of post, copied and pasted different scripts (of course changed the fields to reflect my field names) and none of the scripts worked. At one point, my total hours returned was a "0" If you could so kindly provide a script where I can copy and paste it so my goal can be achieved would be much appreciated. If its not possible, then I guess I will go back to the old fashion way of doing the arithmetic by hand.

Here is what the form looks like, there will be a total of 11 boxes[fields]. I used the [] to represent the boxes:

[Actual Date In] [Date out]
[Actual Time In] [Time out] [Hours/Min]

[Scheduled Date In] [Date out]
[Scheduled Time In] [Time out] [Hours/Min]

Overtime performed...............[Hours/Min]

So to give an example of what it looks like with the dates and times:

Actual Date In: 11/23/10 Date out: 11/24/10
Actual Time In: 1930 Time out: 0600 Total actual hours worked: 10hrs/30min

Scheduled Date In: 11/23/10 Date out: 11/24/10
Scheduled Time In: 2130 Time out: 0600 Total scheduled hours worked: 8hrs/30min

Overtime performed............................................................2hrs/00min

Once again, thank you so much for responding and for taking the time out to read my post.

Jim Salo
Masi
Registered: Sep 18 2008
Posts: 22
George,

OP = ongoing problem? I'm trying to give the man a direction, what are you trying to explain? There's no need to go all the way into epoch with this kind of problem. There are 24 hours in a day and when calculating working hours, there's no need to know which day is it.
If you go to work say 0800 and leave 2000, then working hours can be calculated with ( (20*60 [hours in minutes] + 00 [minutes]) - (08*60 [hours in minutes] + 00 [minutes])) / 60
If you go to work 1915 and leave 0245, then working hours can be calculated with ( ((24+02)*60 + 45) - (19*60 + 15))/60
right? 24+02 comes from the fact that you must calculate 02 hours as 26 since you went to work 19 hours and the clock rounded into 0 at midnight.
You can use the same logic for the scheduled time and you can calculate overtime by simple extraction of worked hours - scheduled hours.

Or have I misunderstood the original problem somehow?
adobeone2010
Registered: Nov 23 2010
Posts: 27
Masi,

Thank you for responding. I think you understand what I need. So, how do I apply this? Is this the script that I use? Sorry for being a noob but I'm not understanding this whole script thing.

Thanks again and Happy Thanksgiving

Jim Salo
Masi
Registered: Sep 18 2008
Posts: 22
I improved the JavaScript a bit to eliminate the bug I talked before. Now set this script into a field calculate method. TextField1 is the time when the person went (or should have went) to work and TextField2 is the time person leaved (or should have leaved). I recommend to study this script a bit to understand on how variables, strings and numbers are used so that you can modify and alter it in future. I added the if-statement so that the calculation is made when user have entered both times in proper format (hhmm).

var vTime1 = TextField1.rawValue;
var vTime2 = TextField2.rawValue;
if (vTime1.length == 4 && vTime2.length == 4) {
var vTime1Minutes = parseInt(vTime1.substring(0, 1))*600 + parseInt(vTime1.substring(1, 2))*60 + parseInt(vTime1.substring(2, 4));
var vTime2Minutes = parseInt(vTime2.substring(0, 1))*600 + parseInt(vTime2.substring(1, 2))*60 + parseInt(vTime2.substring(2, 4));
if (vTime1Minutes > vTime2Minutes)
this.rawValue = (vTime2Minutes+1440 - vTime1Minutes)/60;
else
this.rawValue = (vTime2Minutes - vTime1Minutes)/60;
}

adobeone2010
Registered: Nov 23 2010
Posts: 27
Masi,

Thank you so much, I'm gonna try the script and if all works I will accept this as the answer. Again, thank you so so much for your time and effort for understanding my problem.

Happy Thanksgiving!!!

Jim Salo
adobeone2010
Registered: Nov 23 2010
Posts: 27
Hey Masi,

It seems to work with whole times ex: Time In = 1200 Time Out = 2000 Gives me 8 hours.

However if I use Time In = 2127 Time Out = 0600 Should give me 8:33 however it gives me 8:55

I'm trying to find where it's going wrong but can't find the answer, otherwise it is in the right direction.

Thanks,

Jim Salo
adobeone2010
Registered: Nov 23 2010
Posts: 27
Also forgot to mention that instead of getting a "colon" between the hours and minutes, I'm getting a period instead, how do fix that too?
adobeone2010
Registered: Nov 23 2010
Posts: 27
I found a script from the web but not sure how to apply to get the desired results.

350 minutes is 5 hours 50 minutes

var Hours = Math.floor(350/60);
var Minutes = 350%60;
var Time = Hours + ":" + Minutes;
Masi
Registered: Sep 18 2008
Posts: 22
Accepted Answer
adobeone2010 wrote:
var Hours = Math.floor(350/60);
var Minutes = 350%60;
var Time = Hours + ":" + Minutes;
Yes you can use that script to format the result:
var vTime1 = TextField1.rawValue;
var vTime2 = TextField2.rawValue;
if (vTime1.length == 4 && vTime2.length == 4) {
var vTime1Minutes = parseInt(vTime1.substring(0, 1))*600 + parseInt(vTime1.substring(1, 2))*60 + parseInt(vTime1.substring(2, 4));
var vTime2Minutes = parseInt(vTime2.substring(0, 1))*600 + parseInt(vTime2.substring(1, 2))*60 + parseInt(vTime2.substring(2, 4));
if (vTime1Minutes > vTime2Minutes)
vTime1 = vTime2Minutes+1440 - vTime1Minutes;
else
vTime1 = vTime2Minutes - vTime1Minutes;
}
var Hours = Math.floor(vTime1/60);
var Minutes = vTime1%60;
this.rawValue = Hours + ":" + Minutes;

As you can see, I used vTime1 variable to save the calculated minutes and used it then in the script that you had found.
adobeone2010
Registered: Nov 23 2010
Posts: 27
YES!!! It works.........Thank You So So So Much

BTW, did I thank you enough?

Thanks again,

Jim "Happy" Salo
adobeone2010
Registered: Nov 23 2010
Posts: 27
Hey Masi,

Though the script works as expected, in my haste to complete the form, users are coming back with a glitch. Instead of displaying a two digit for the minutes, it's giving a one digit minute when the minutes are less than 10. ex: "2:04" is coming in as "2:4" I found a script and tried to apply with no luck.

Typing this code...

var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()

if (minutes < 10)
minutes = "0" + minutes

document.write("" + hours + ":" + minutes + " " + "")Results in this...
13:04


I also found this script as well:

var curr_min = d.getMinutes();

curr_min = curr_min + "";

if (curr_min.length == 1)
{
curr_min = "0" + curr_min;
}

Cheers,

Jim Salo
Masi
Registered: Sep 18 2008
Posts: 22
Does this fix the issue with minutes being less than 10?

var vTime1 = TextField1.rawValue;
var vTime2 = TextField2.rawValue;
if (vTime1.length == 4 && vTime2.length == 4) {
var vTime1Minutes = parseInt(vTime1.substring(0, 1))*600 + parseInt(vTime1.substring(1, 2))*60 + parseInt(vTime1.substring(2, 4));
var vTime2Minutes = parseInt(vTime2.substring(0, 1))*600 + parseInt(vTime2.substring(1, 2))*60 + parseInt(vTime2.substring(2, 4));
if (vTime1Minutes > vTime2Minutes)
vTime1 = vTime2Minutes+1440 - vTime1Minutes;
else
vTime1 = vTime2Minutes - vTime1Minutes;
}
var Hours = Math.floor(vTime1/60);
var Minutes = vTime1%60;
if (Minutes < 10)
Minutes = "0" + Minutes;
this.rawValue = Hours + ":" + Minutes;

adobeone2010
Registered: Nov 23 2010
Posts: 27
Yes it most certainly do.

Thank you once again, you've been extremely helpful.

Cheers,

JIm Salo
adobeone2010
Registered: Nov 23 2010
Posts: 27
Hey Masi,

Thanks for all your help. Wanted to create a New Topic but since this one has been resolved was afraid that I would get wrong solutions to the Great Script you put together. Anyway, now that the fields add up (Time In / Time Out) to give total hours. I actually have another set of fields doing the same thing giving me total hours. Now, I want to take those both total hours and calculate them to give me the Total Over Time hours performed.

Ex: set 1 = Time In (2100) Time Out (0800) Total Hours = (10 hours)
set 2 = Time In (2100) Time Out (0600) Total Hours = (8 hours)

Now I need the populated fields of Total Hours to give me the difference in a new field box, so in this case the Total Over Time hours performed would be 2 hours.

I'd figured I would just copy and paste your script (making the necessary changes just as I did for "set 2", which works as expected) but the result I get for the Total Over Time hours performed is "NaN:NaN"

Can you please guide me on this matter,

Thank you,

Jim Salo

Masi
Registered: Sep 18 2008
Posts: 22
So you have values in the form hh:mm and the script expects the value to be in form hhmm. Try to change the form into hhmm at the start of the script like this:
var vTime1 = TotalHours1.rawValue;
var vTime2 = TotalHours2.rawValue;
vTime1 = vTime1.replace(":", "");
vTime2 = vTime2.replace(":", "");


Rest of the script should be the same..
adobeone2010
Registered: Nov 23 2010
Posts: 27
Hey Masi,

Sorry Masi, forgot to mention that all my times are with the colon, I should have included that. Anyway, here is the script:

var vTime1 = actualtotalhrs.rawValue;
var vTime2 = schtotalhrs.rawValue;
vTime1 = vTime1.replace(":", "");
vTime2 = vTime2.replace(":", "");
if (vTime1.length == 4 && vTime2.length == 4) {
var vTime1Minutes = parseInt(vTime1.substring(0, 1))*600 + parseInt(vTime1.substring(1, 2))*60 + parseInt(vTime1.substring(2, 4));
var vTime2Minutes = parseInt(vTime2.substring(0, 1))*600 + parseInt(vTime2.substring(1, 2))*60 + parseInt(vTime2.substring(2, 4));
if (vTime1Minutes > vTime2Minutes)
vTime1 = vTime2Minutes+1440 - vTime1Minutes;
else
vTime1 = vTime2Minutes - vTime1Minutes;
}
var Hours = Math.floor(vTime1/60);
var Minutes = vTime1%60;
if (Minutes < 10)
Minutes = "0" + Minutes;
this.rawValue = Hours + ":" + Minutes;

Here is the result I'm getting


actual hours worked is 15:15
scheduled hours worked is 08:30
total overtime hours is 25:15 (it should be 06:45)

as you can see, it's coming back with the incorrect hours and minutes.

Please note: your previous scripts are working flawlessly....I thank you for that.

Cheers,

Jim Salo
adobeone2010
Registered: Nov 23 2010
Posts: 27
Hey Masi,

Never mind, I found out where I was going wrong. All is working perfect. I ran the form every possible way and came back with zero errors.

I thank you,

Cheers,

Jim Salo

Masi
Registered: Sep 18 2008
Posts: 22
change it this way:
var vTime1 = schtotalhrs.rawValue;
var vTime2 = actualtotalhrs.rawValue;

and replace this:
if (vTime1Minutes > vTime2Minutes)
vTime1 = vTime2Minutes+1440 - vTime1Minutes;
else
vTime1 = vTime2Minutes - vTime1Minutes;
}

with just this (since now we don't need to worry about times over midnight):
vTime1 = vTime2Minutes - vTime1Minutes;

adobeone2010
Registered: Nov 23 2010
Posts: 27
Hey Masi,

Guess we were both typing at the same time....lol

Thank you again so so much.

Cheers,

Jim Salo
adobeone2010
Registered: Nov 23 2010
Posts: 27
thanks
adobeone2010
Registered: Nov 23 2010
Posts: 27
Just thought I'd share what the problem was and thank Masi so very much for all the help in putting the script together.

var Hours = Math.floor(vTime1/60);
var Minutes = vTime1%60;
if (Minutes < 10)
Minutes = "0" + Minutes;
this.rawValue = Hours + ":" + Minutes;

The reason why my calculations were so wacky as posted on #19 was because my hours were in single digits.
ex; "9" should be "09" once I fixed this, my calculations were correct. This is what I added

if (Hours < 10)
Hours = "0" + Hours;

Hope that helps anyone else.

Now if I can just get 24 hours to read as "24" and not "00" hours, I'll be one happy lad.

Cheers,

Jim Salo
adobeone2010
Registered: Nov 23 2010
Posts: 27
Well, seek and one shall have the answer. I fixed the 24 (24:00) showing up as 00 (00:00)

Thought I'd share the script:

var Hours = Math.floor(vTime1/60);
var Minutes = vTime1%60;
if (Hours == 0)
Hours = 24;
if (Hours <= 9)
Hours = "0" + Hours;
if (Minutes < 10)
Minutes = "0" + Minutes;
this.rawValue = Hours + ":" + Minutes;

The above script (with the help of Masi) fixed the single digit hour by adding a leading "0" and fixed the 24 showing up as 00:00. I now get 24:00

My apologies for posting on an answered post but I felt obligated in showing the solution in case anyone in the future has a similar problem.

Cheers,

Jim Salo
mcfar56
Registered: Mar 10 2011
Posts: 6
Well, I am still lost. Yes, I am a noob. I have tried everyone's examples but I must just be dumb. I have a form that is similar. I have In1, Out1 and Time1 (all the way to 20 fields). All I need to do is get the time on the job. Example: in 0900, out 1000 and have the result of 1 hr or 1:00. Or 0915 in and 1030 out and get the result of 1 hr and 15 min or 1:15. I am realllllly lost here. I also want to total the hrs worked in another field. Can anyone help? Rich
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
How skilled are you with programming?
How skilled are you with JavaScirpt?

Have you worked out a pseudo code for the computation of the difference in time?

The values for the input time fields consist of the number of hours 0(-24), the ":" separator, and the number of minutes 0-60. This is a character string value and not a numeric value. Maybe we need to convert this string to some sort of time value.

Do you have an idea on how to compute time difference?
You can not just subtract hours and minutes with the separator, you need to subtract just hours or just minutes.
Do we need to get the time values in the same time unit for calculations? You can also not just add the strings, JavaScirpt will concatenate the values.

Lets start with the 'Time1' field. You will need to use a custom calculation script for the calculation. Assuming the format for the 'In1' and 'Out1' fields are set to 'Time' 'HH:MM', you need to get the values of the fields. The 'Time1' field will have a format of 'None'.

// names for the fields to use in the calculation
var cOut = 'Out1';
var cIn = 'In1';

// common code for computing difference
// clear the difference field for Time1
event.value = '';
// get the value of the out time field
var sOut = this.getField(cOut).value;
// get the value of the in time field
var sIn = this.getField(cIn).value;
// compute if we have
if(sOut != '' && sIn != '') {
// convert sOut to hours
// split value into hours and minutes
var aOut = sOut.split(":");
// convert out minutes to fraction of hours and add to out hours
var nOut = (aOut[1] / 60) + Number(aOut[0]);
// convert the sIn value to hours
var aIn = sIn.split(":");
// convert in minutes to fraction of hours and add to in hours
var nIn = (aIn[1] / 60) + Number(aIn[0]);
// compute the difference in hocus
nDiff = nOut - nIn;
// get the hours and minutes from the difference
// hours for difference
var nHrs = Math.floor(nDiff);
// minutes from the difference
// get the decimal minutes
var nMins = nDiff - nHrs;
// convert decimal minutes to minutes
nMins = nMins * 60;
// format minutes to string with a leading zero
nMins = util.printf("%,002.0f", nMins);
// build formatted result string
event.value = nHrs + ':' + nMins;
} // end computation of time difference

The above script has additional code to make arrays from string value, force a value to a number, and round and fromat a value to a string with a leading zero.

George Kaiser

mcfar56
Registered: Mar 10 2011
Posts: 6
Thank you, after pulling the rest of my hair out..... Thank you
mcfar56
Registered: Mar 10 2011
Posts: 6
Thank you, after pulling the rest of my hair out..... Thank you
mcfar56
Registered: Mar 10 2011
Posts: 6
Thank you, after pulling the rest of my hair out..... Thank you