Skip to main content
Participating Frequently
March 6, 2025
Answered

I need help calculating hours when going between day like 5 am to 3 am the next day...

  • March 6, 2025
  • 1 reply
  • 609 views

Here is my syntax that works within 12 hours but when I cross over to the next day... It won't work. Help please!!! Thanks in advance...

 

if ((this.getField("ShiftEND1").value.length == 0) || (this.getField("ShiftSTART1").value.length == 0)) {
event.value = " 0 ";
}
else{

var timefinished = this.getField("ShiftEND1").value;
var timestarted = this.getField("ShiftSTART1").value;

var datetimefinished = new Date('1970/01/01' + " " + timefinished);
var datetimestarted = new Date('1970/01/01' + " " + timestarted);

var difflnMilliSeconds = Math.abs(datetimefinished - datetimestarted)/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/.6 + " ";
}

Correct answer Nesa Nurani

Try this:

 

if ((this.getField("ShiftEND1").value.length == 0) || (this.getField("ShiftSTART1").value.length == 0)) {
    event.value = " 0 ";
} else {
    var timefinished = this.getField("ShiftEND1").value;
    var timestarted = this.getField("ShiftSTART1").value;

    var datetimestarted = new Date('1970/01/01 ' + timestarted);
    var datetimefinished = new Date('1970/01/01 ' + timefinished);

    if (datetimefinished < datetimestarted) {
        datetimefinished.setDate(datetimefinished.getDate() + 1);
    }

    var difflnMilliSeconds = (datetimefinished - datetimestarted) / 1000;
    var hours = Math.floor(difflnMilliSeconds / 3600);
    var minutes = Math.floor((difflnMilliSeconds % 3600) / 60);

    var decimalHours = hours + (minutes / 60);

    event.value = decimalHours.toFixed(2);
}

 

1 reply

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
March 6, 2025

Try this:

 

if ((this.getField("ShiftEND1").value.length == 0) || (this.getField("ShiftSTART1").value.length == 0)) {
    event.value = " 0 ";
} else {
    var timefinished = this.getField("ShiftEND1").value;
    var timestarted = this.getField("ShiftSTART1").value;

    var datetimestarted = new Date('1970/01/01 ' + timestarted);
    var datetimefinished = new Date('1970/01/01 ' + timefinished);

    if (datetimefinished < datetimestarted) {
        datetimefinished.setDate(datetimefinished.getDate() + 1);
    }

    var difflnMilliSeconds = (datetimefinished - datetimestarted) / 1000;
    var hours = Math.floor(difflnMilliSeconds / 3600);
    var minutes = Math.floor((difflnMilliSeconds % 3600) / 60);

    var decimalHours = hours + (minutes / 60);

    event.value = decimalHours.toFixed(2);
}

 

KellermAuthor
Participating Frequently
March 6, 2025

YOU ARE AWESOME!!!!!!!!!

Thanks you soooooo much!!!!

Blessings to you!