Copy link to clipboard
Copied
I'm trying to calculate two time to the get total of hour worked, For example:
2:00 - 14:00 = 12 hrs
0:00 - 0:00 = 24 hrs
13:50 - 14:00 = .50 hrs
I am a complete, never used Java before, novice. Here is what I put together.
if ((this.getField("End time").value.length == 0) || (this.getField("Start time").value.length == 0)) {
event.value = " 24.0";
}
else{
var timefinished = this.getField("End Time").value;
var timestarted = this.getField("Start Time").value;
var difflnMilliSeconds = Math.abs(timfinshed - timestarted)/1000;
// calculate hours
var hours = Math.floor(difflnMilliSeconds / 3600) % 24;
difflnMilliSeconds -= hours *3600;
// calculate minutes
var minutes = Math.floor(difflnMilliSeconds / 60) % 60;
difflnMilliSeconds -= minutes * 60;
// set field value to the difference
event.value = hours + "." + minutes;
}
Copy link to clipboard
Copied
The times in your form fields are text that represents a time. This text needs to be converted into a date object, or into some other numerical fomat that can be used for a time calculation.
You can read about time/data handling here:
https://www.pdfscripting.com/public/Date-and-Time-Handling.cfm
'https://acrobatusers.com/tutorials/date_time_part1/
https://acrobatusers.com/tutorials/date_time_part2/
Also, this same question has been asked many times and there are several solutions on this forum
Here's a search for "hours calculation"
Copy link to clipboard
Copied
I did a lot of research and almost have the below working. There are 3 issues
1. When I do a half hr it calculates as a whole number, i.e I have 1:00 - 2:30 shows 1.30 rather than 1.5
2. When there is no data it shows 24 hrs. I want it blank
3. When I put in 0:00 - 0:00 it shows 0 hrs. I want it to show 24 hrs.
I appreciate your help.
if ((this.getField("End").value.length == 0) || (this.getField("Start").value.length == 0)) {
event.value = " 24.0";
}
else{
// get the values of the fields
var sStart = this.getField ("Start").value;
var sEnd = this.getField("End").value;
var sDate = "1-Jan-1970 ";
// compute only if we have values
if(sStart != "" && sEnd != "");
// add a formatted date value to the time field values
if (sStart > sEnd) {sDate = "2-Jan-1970 "}
sStart = "1-Jan-1970 " + sStart;
sEnd = sDate + sEnd;
// convert the date time strings to date object
var oStart = util.scand("d-mmm-yyyy h:MM tt", sStart);
var oEnd = util.scand("d-mmm-yyyy h:MM tt", sEnd);
// convert date object to milliseconds since 1-Jan-1970
var nStart = oStart.getTime();
var nEnd = oEnd.getTime();
// compute the difference in milliseconds
var nDiff = nEnd - nStart;
// convert to seconds
var nSeconds = Math.floor(nDiff / 1000);
// compute the whole hours from the minutes
var nHrs = Math.floor(nSeconds / (60 * 60));
// compute the remaining minutes
var nMins = (nSeconds / 60) % 60;
// set field value to the difference
event.value = nHrs + "." + nMins;
}
Copy link to clipboard
Copied
Got the null value worked out. Still need the 24 hr and the difference when it is a partial hour.
// No Data
if ((this.getField("End4").value.length == "") || (this.getField("Start4").value.length == "")) {
event.value = "";
}
else{
if ((this.getField("End4").value.length == "0") || (this.getField("Start4").value.length == "0")) {
event.value = "24";
}
else{
Copy link to clipboard
Copied
To show .5 replace:
event.value = nHrs + "." + nMins;
with:
if(nMins == 0)
event.value = nHrs + "." + nMins;
else
event.value = nHrs + nMins/60;
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more