4 Replies Latest reply on Apr 9, 2017 10:11 AM by gkaiseril

# I have a timesheet where TimeOut - TimeIn - Break = Total Hours.  I can get this to work but, I need an if clause for working past midnight.  If (TimeOut<TimeIn) +24 or If (TotalHours<0) +24.  I do not know JavaScript (obviously).

I have a timesheet where TimeOut - TimeIn - Break = Total Hours.  I can get this to work but, I need an if clause for working past midnight.  If (TimeOut<TimeIn) +24 or If (TotalHours<0) +24.  I do not know JavaScript (obviously).

• ###### 1. Re: I have a timesheet where TimeOut - TimeIn - Break = Total Hours.  I can get this to work but, I need an if clause for working past midnight.  If (TimeOut<TimeIn) +24 or If (TotalHours<0) +24.  I do not know JavaScript (obviously).

Can you post the code you've used so far?

• ###### 2. Re: I have a timesheet where TimeOut - TimeIn - Break = Total Hours.  I can get this to work but, I need an if clause for working past midnight.  If (TimeOut<TimeIn) +24 or If (TotalHours<0) +24.  I do not know JavaScript (obviously).

I used the Simplified field notation in the Text Field Properties tab as here:

But it comes back with a negative number:

I have always used Excel and can figure out how to do pretty much anything (self-taught) with spreadsheets but, of course, javascript is different.  I have tried many scripts I have found online but, as I do not really understand the language, am not sure what I am doing wrong. One is:

var result = endMin - startMin;

`if (result < 0 ) result = (24*60) + result;Most are much more intricate and quite lengthy. Thank you!`
• ###### 3. Re: I have a timesheet where TimeOut - TimeIn - Break = Total Hours.  I can get this to work but, I need an if clause for working past midnight.  If (TimeOut<TimeIn) +24 or If (TotalHours<0) +24.  I do not know JavaScript (obviously).

Yeah, that's not going to work, since these are not regular number fields... You would basically need to convert them into Date objects and then calculate the difference between those variables. This issue was discussed here many times in the past. Try searching the forum for "time calculation" or something like that.

If you're interested in setting it up without having to write any code I've developed a tool that allows you to do it very easily, and it will also handle dates that are on different dates correctly. You can find it here: Custom-made Adobe Scripts: Acrobat -- Calculate Time Differences in a Worksheet

• ###### 4. Re: I have a timesheet where TimeOut - TimeIn - Break = Total Hours.  I can get this to work but, I need an if clause for working past midnight.  If (TimeOut<TimeIn) +24 or If (TotalHours<0) +24.  I do not know JavaScript (obviously).

As noted, your times are not numbers but character string which need to be converted to some type of number before any calculations can be performed. Because of Daylight Savings Time or Summer Time causing some time values to be locally not contiguous, one should also include the date for the measured time period. JavaScript uses a date object and this object can perform many date and time manipulations by various methods. The Date object uses a base starting point, Epoch date, of January 1, 1970 12:00 UTC, and assigns a sequential number to every millisecond form that date. The use of UTC time is a standard for transportation, communications, and military purposes. It is the time at England's Greenwich Royal Observatory and then local time is computed by a time zone offset. This is also how your computer tracks time and why when one sets up a computer one is asked for the time zone offset and location since some locations within a time zone area may not observe Daylight Savings Time. With this information your computer can determine when DST becomes active or ends for your location. JavaScript uses the computer to adjust as needed for DST.

Acrobat JS has the util.scand method for converting date and time strings into the JS Date object. Then one can use the getTime method of JS to obtain the number of milliseconds since the Epoch date. With this value for the 2 times one can compute the number of milliseconds  between the two times and then convert that value to your chosen time unit.