Skip to main content
New Participant
July 5, 2020
Question

24 hr time format is not giving correct values for the time difference calculation

  • July 5, 2020
  • 2 replies
  • 2155 views

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;

 

This topic has been closed for replies.

2 replies

Bernd Alheit
Community Expert
July 6, 2020

Your script is wrong. When the difference of the minutes is negativ add 60 minutes and subtract 1 hour.

New Participant
July 7, 2020

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. 

Bernd Alheit
Community Expert
July 7, 2020

No, try 8:30 and 10:15

 

Inspiring
July 6, 2020

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.