Copy link to clipboard
Copied
I have the following code for my pdf time sheet Time In , Time Out in 24 hr time format. When I use 8:30 AM -10:00 AM it give correct value as 1.5. But when I use 11:30 am - 12:15 AM I am expecting 0.75 but I am NOT getting that value. Please advise and help me out here.
var start1 = this.getField("Recall1_Wk1_Thurs_TimeIn").valueAsString;
var finish1 = this.getField("Recall1_Wk1_Thurs_TimeOut").valueAsString;
var start2 = this.getField("Recall2_Wk1_Thurs_TimeIn").valueAsString;
var finish2 = this.getField("Recall2_Wk1_Thurs_TimeOut").valueAsString;
var totalTime = 0;
if (start1!="" && finish1!="") {
var startArr = start1.split(":");
var finishArr = finish1.split(":");
var hourDiff = Math.abs(finishArr[0] - startArr[0]);
var minDiff = Math.floor((Math.abs(finishArr[1] - startArr[1]) / 60)*60);
totalTime+=(hourDiff*60)+minDiff;
}
if (start2!="" && finish2!="") {
var startArr = start2.split(":");
var finishArr = finish2.split(":");
var hourDiff = Math.abs(finishArr[0] - startArr[0]);
var minDiff = Math.floor((Math.abs(finishArr[1] - startArr[1]) / 60)*60);
totalTime+=(hourDiff*60)+minDiff;
}
var totalTimeHours = Math.floor(totalTime/60);
var totalTimeMinutes = (totalTime-(totalTimeHours*60))/60;
var output = totalTimeHours+totalTimeMinutes;
event.value = output;
Copy link to clipboard
Copied
The first day has hours 0000 to 2359 the second day has hours 2400 to 4759. You need to include the start date and end date along with the time. You can then use the util.scand to convert the date and time strings yo JavaScript 's date object which can then allow you to make calculation calculation in milliseconds. Then it is an easy conversion to hours and minutes. Using the date object will also adjust for Daylight Savings Time if needed.
Copy link to clipboard
Copied
Your script is wrong. When the difference of the minutes is negativ add 60 minutes and subtract 1 hour.
Copy link to clipboard
Copied
I am sorry I didn't get what you mean. Can you please provide me the correct script for teh above issue. It only fails when the time for finsih1 and finish2 is set to 12 , otherwise it works perfectly for all teh other times. I appreciate your help.
Copy link to clipboard
Copied
No, try 8:30 and 10:15
Copy link to clipboard
Copied
OK ...I get it what you mean. It gave me 2.25 where as I was expecting 1.75, Can you please help me with the code. I am only admin not a coding person.
Copy link to clipboard
Copied
To get code written you need a coding person. We can help you with your study, but you may find us reluctant to just do your work for you.
Copy link to clipboard
Copied
How does you got the wrong script?
Copy link to clipboard
Copied
What Bernd meant earlier is that your script is basically missing an important part of this calculation.
The first thing I noticed is the same observation as Gkaiseril.
You cannot use AM/PM format if you're using military time format, that is a 24 hour period.
In AM/PM format you deal with hours 1 through 12 and the AM or PM indicator lets you know if it is morning or evening times. In military time format you deal with 24 hour period. Formats are not expressed as 9:00 for example, It has the extended "0" for hours and minutes. Morning begins at 00:00 while Midnight end at 24:00 not 12:00 for example.
So, for example, you will run into conflict with your script because 12:30 (at noon) - 21:05 (9:05 PM) is not greater than the end time.
To do it correctly it will have to be expressed like this:
If starting minutes are greater than the minutes of ending time just substract hours and add the minutes normally (a resulting negative value of the hours shouldn't matter to us, but will will matter in you javascript equation(s) though. So pay attention to those negative totals.
If starting minutes are not greater than the minutes of the ending time, then follow the rule of thumb:
Substract one hour from the ending time hours.
Add 60 minutes to the starting time minutes, then substract the total hours and minutes
That would be the toal in hours and minutes between the two times.
See more here how to do it manually: How To Add and Subtract Time in Hours and Minutes
Your current script not only disregards what happens when the starting time is less than the ending time, but also, disregards what happen in the event that would normally take place at noon (12:00) versus midnight (24:00).
And since you've splitted the resulting hours and minutes as separate strings Bernd pointed out that your script is incomplete, because is missing a basic time arithmetic formula.
When the time is expressed in a numerical manner to substract hour and minutes between two given times, if the hour and minutes of the starting time are less than the hours and minutes of the ending time, you need to use this equation: https://www.aaamath.com/B/meatsbhm.htm
Look in the example in that website. It says:
What is 4 hours 11 minutes minus 2 hours 47 minutes.
The original number of minutes (11) is less than the 47 minutes being subtracted so:
Subtract 1 from the original number of hours. (4 hours - 1 hour = 3 hours)
Increase the number of minutes by 60. (11 minutes + 60 minutes = 71 minutes)
Subtract the minutes. (71 minutes - 47 minutes = 24 minutes)
Subtract the hours. (3 hours - 2 hours = 1 hour)
Answer: 1 hour 24 minutes
Using the arithmetic rule posted above, the part you should be looking to fix in your script is here:
var hourDiff = Math.abs(finishArr[0] - startArr[0]);
var minDiff = Math.floor((Math.abs(finishArr[1] - startArr[1]) / 60)*60);
You may need to also implement a conditional statement, like Bernd spotted. See this this other part in your script:
if (start1!="" && finish1!="") {
It should provide a condition to handle what happens if the starting time is not greater than the ending time, because if it's not, then you have to consider the rule of thumb of substracting 1 from the total hours of the ending time, then add 60 to the total number of minutes of the starting time. Only then you'll be able to totalize correctly. So consider something like:
// declare all your variables first
if ( (startArr !="" && finishArr !="") {
// state a condition to handle what happens if the starting time is less than the ending time
if (start1.length < finish1.length) {
// then put your code here
} else {
// do something else or declare no result to display if the conditions above are not met.
//For example, if starting time is greater than ending time then do
if (start1.length > finish1.length) {
//and put the code here to handle that condition
}
}
}
Copy link to clipboard
Copied
Good point. In fact, 11:30 am - 12:15 AM is 12 hours and 45 minutes, since 11:30 am is just before noon, and 12:15 AM is just after midnight.
Copy link to clipboard
Copied
I feel like I owe you this link, which is one of Thom Parker's excellent JavaScript Tutorials: https://acrobatusers.com/tutorials/date_time_part2/
You will need to read a bit to grasp the concepts, but this is one of the best tutorials (if not the best) that you will find here in the forums to work around time and date in your PDFs using Acrobat JavaScript..