Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Calculating between two times to get total hours

New Here ,
Feb 06, 2023 Feb 06, 2023

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;
}

TOPICS
PDF forms
3.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 06, 2023 Feb 06, 2023

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"

https://community.adobe.com/t5/forums/searchpage/tab/message?q=Hours%20calculation&noSynonym=false&c...

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 15, 2023 Feb 15, 2023

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;
}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 15, 2023 Feb 15, 2023

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{

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 15, 2023 Feb 15, 2023
LATEST

To show .5 replace:

event.value = nHrs + "." + nMins;

with:

if(nMins == 0)
event.value = nHrs + "." + nMins;
else
event.value = nHrs + nMins/60;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines