Hrs worked between two dates crossing over midnight

Joined
Feb 10, 2023
Messages
1
Reaction score
0
I'm a absolute novice at Javascript coding. Can you look at my code to see why it is not working?

The results I am trying to get is:
1:00 to 3:00 equals 2
22:00 to 1:00 equals 3
etc.

if ((this.getField("End time").value.length == 0) || (this.getField("Start time").value.length == 0)) {
event.value = " 24.0";
}
else{

var time1 = this.getField("End Time").value;
var time2 = this.getField("Start Time").value;

// convert to date
var datetime1 = new Date('1970/01/01 ' + time1);
var datetime2 = new Date('1970/01/01 ' + time2);

var diffInMilliSeconds = Math.abs(datetime1 - datetime2) / 1000;

// calculate hours
var hours = Math.floor(diffInMilliSeconds / 3600) % 24;
diffInMilliSeconds -= hours * 3600;

// calculate minutes
var minutes = Math.floor(diffInMilliSeconds / 60) % 60;
diffInMilliSeconds -= minutes * 60;

// set field value to the difference
event.value =hours + ":" +minutes;
}

var result;

if (Time1 < Time2) {
var minutesPerDay = 24*60;
result = minutesPerDay - Time2; // Minutes till midnight
result += Time2; // Minutes in the next day
} else {
result = Time1 - Time2;
}

var minutesElapsed = result % 60;
var hoursElapsed = (result - minutesElapsed) / 60;

alert ( "Elapsed Time : " + hoursElapsed + ":" + (minutesElapsed < 10 ?
'0'+minutesElapsed : minutesElapsed) ) ;
 
Joined
Jan 30, 2023
Messages
107
Reaction score
13
The code you posted has several issues that are preventing it from working as intended. Here are some of the problems:

  1. Inconsistent field name references: The field name for the start time is referenced as "Start Time" in one part of the code, and "Start time" in another. Similarly, the end time is referred to as "End Time" and "End time". Make sure to use a consistent naming convention for the field names throughout the code.
  2. Incorrect calculation of time difference: The calculation of the difference between the start and end times seems to be done correctly using the JavaScript Date object. However, the calculation of the elapsed time using result is incorrect, as it does not take into account the case where the end time is less than the start time (i.e., when the end time is in the next day).
  3. Invalid time format: The time format used in the code, "hours:minutes", is not a valid time format in JavaScript. To correctly represent a time value, you should use the toLocaleTimeString() method on the Date object to format the time as desired.
  4. Unused variables: The variables time1 and time2 are declared but not used in the code, and the variable result is set but not used in the calculation of the elapsed time.
Here is a corrected version of your code:

JavaScript:
if ((this.getField("End Time").value.length == 0) || (this.getField("Start Time").value.length == 0)) {
    event.value = "24.0";
} else {
    var endTime = this.getField("End Time").value;
    var startTime = this.getField("Start Time").value;
 
    // convert to date
    var endDate = new Date('1970/01/01 ' + endTime);
    var startDate = new Date('1970/01/01 ' + startTime);
 
    var diffInMilliSeconds = Math.abs(endDate - startDate) / 1000;
 
    // calculate hours
    var hours = Math.floor(diffInMilliSeconds / 3600) % 24;
    diffInMilliSeconds -= hours * 3600;
 
    // calculate minutes
    var minutes = Math.floor(diffInMilliSeconds / 60) % 60;
 
    // set field value to the difference
    var formattedTime = endDate.toLocaleTimeString("en-US", {hour: '2-digit', minute:'2-digit'});
    event.value = formattedTime;
 
    // calculate elapsed time
    var elapsedTime = ((hours + 24) - (startDate.getHours() + (startDate.getMinutes() / 60))) % 24;
    var hoursElapsed = Math.floor(elapsedTime);
    var minutesElapsed = Math.floor((elapsedTime - hoursElapsed) * 60);
 
    alert("Elapsed Time: " + hoursElapsed + ":" + (minutesElapsed < 10 ? '0' + minutesElapsed : minutesElapsed));
}
 
Joined
Sep 21, 2022
Messages
121
Reaction score
15
The code you posted has several issues that are preventing it from working as intended. Here are some of the problems:

  1. Inconsistent field name references: The field name for the start time is referenced as "Start Time" in one part of the code, and "Start time" in another. Similarly, the end time is referred to as "End Time" and "End time". Make sure to use a consistent naming convention for the field names throughout the code.
  2. Incorrect calculation of time difference: The calculation of the difference between the start and end times seems to be done correctly using the JavaScript Date object. However, the calculation of the elapsed time using result is incorrect, as it does not take into account the case where the end time is less than the start time (i.e., when the end time is in the next day).
  3. Invalid time format: The time format used in the code, "hours:minutes", is not a valid time format in JavaScript. To correctly represent a time value, you should use the toLocaleTimeString() method on the Date object to format the time as desired.
  4. Unused variables: The variables time1 and time2 are declared but not used in the code, and the variable result is set but not used in the calculation of the elapsed time.
Here is a corrected version of your code:

JavaScript:
if ((this.getField("End Time").value.length == 0) || (this.getField("Start Time").value.length == 0)) {
    event.value = "24.0";
} else {
    var endTime = this.getField("End Time").value;
    var startTime = this.getField("Start Time").value;
 
    // convert to date
    var endDate = new Date('1970/01/01 ' + endTime);
    var startDate = new Date('1970/01/01 ' + startTime);
 
    var diffInMilliSeconds = Math.abs(endDate - startDate) / 1000;
 
    // calculate hours
    var hours = Math.floor(diffInMilliSeconds / 3600) % 24;
    diffInMilliSeconds -= hours * 3600;
 
    // calculate minutes
    var minutes = Math.floor(diffInMilliSeconds / 60) % 60;
 
    // set field value to the difference
    var formattedTime = endDate.toLocaleTimeString("en-US", {hour: '2-digit', minute:'2-digit'});
    event.value = formattedTime;
 
    // calculate elapsed time
    var elapsedTime = ((hours + 24) - (startDate.getHours() + (startDate.getMinutes() / 60))) % 24;
    var hoursElapsed = Math.floor(elapsedTime);
    var minutesElapsed = Math.floor((elapsedTime - hoursElapsed) * 60);
 
    alert("Elapsed Time: " + hoursElapsed + ":" + (minutesElapsed < 10 ? '0' + minutesElapsed : minutesElapsed));
}
This logic is wrong.
 
Joined
Sep 21, 2022
Messages
121
Reaction score
15
Convert the start and end times from however they're currently stored into numbers.

use 6 integer variables

SH starting hour 0 to 23
SM starting minute 0 to 59
EH ending hour 0 to 23
EM ending minute 0 to 59
DH duration hours
DM duration minutes

Perform a simple calculation.

Convert the numeric duration into whatever format is convenient.

No need for any dates, abs(), floor(), /, *, %
Code:
pseudocode:

REM duration = end - start

IF EM < SM THEN
 EM += 60
 EH -= 1
ENDIF
IF EH < SH THEN
 EH += 24
ENDIF
DH = EH - SH
DM = EM - SM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top