0

i am trying to find the difference for end time and start time, followed by adding all the time difference

may i know how can i do so?

the code is as followed

function THcheck() {
  var a, s, timeDiff, hr = 0;
  var hdate, startTime, endTime, totalTime, timeDiff;
  var output = "Date   StartTime:  EndTime:  TimeDiff <br>";
  var msg = "";

  var DStime, DEtime;

  var e = document.getElementsByTagName('input');

  for (a = 0; e !== a; a++) {
    if (e[a].type == "time" && e[a].name == "THStime[]") {
      if (e[a].value && e[a].value !== "") {

        startTime = e[a].value;
        endTime = e[a + 1].value;
        hdate = e[a - 1].value

        alert(hdate + " " + startTime + " " + endTime);

        timeDiff = endTime - startTime;

        alert(timeDiff);

        hr = parseInt(timeDiff.asHours());

        alert(timeDiff);

        totalTime += timeDiff;

        alert(totalTime);


        output += hdate + "   " + startTime + "   " + endTime + "   " + timeDiff + "<br>";


        if (hr >= 24) {
          msg = "<br> Exceed 24 hrs! ";
        }

      }
    }
  }
  alert(output + " Total time: " + totalTime + msg);
  return true;
}

thanks in advance for your kind assistance and help on this!

ElSheikh
  • 321
  • 6
  • 28
Anzu Pang
  • 3
  • 1

1 Answers1

0

I think you need to parse the hours first, converting from string to date and then convert the dates to milliseconds and use the milliseconds for the difference calculation. After this you convert the difference milliseconds into hours.

Here is some sample code which performs these steps:

const dateRegex = /(\d{2}):(\d{2})/;

function diffDatesInHours(d1Str, d2Str) {
  const r1 = dateRegex.exec(d1Str);
  const r2 = dateRegex.exec(d2Str);
  if (!checkDate(r1)) {
    console.log("First date format incorrect: " + d1Str);
    return null;
  }
  if (!checkDate(r2)) {
    console.log("Second date format incorrect: " + d2Str);
    return null;
  }
  const d1 = createDateFrom(r1);
  const d2 = createDateFrom(r2);
  const diff = d1.getTime() - d2.getTime();
  return Math.abs(diff) / (1000 * 60 * 60);
}

function checkDate(r) {
  if (r === null) {
    return null;
  }
  return r.length > 0;
}

function createDateFrom(r) {
  let date = new Date();
  date.setHours(r[1], r[2]);
  return date;
}

console.log(diffDatesInHours("09:30", "21:00"));
console.log(diffDatesInHours("09:30", "21:"));
gil.fernandes
  • 12,978
  • 5
  • 63
  • 76
  • Hi, Thanks for the active comment, but i am getting the Start time and end time from the user using the the program requirement is not to have any link to the internet as well as no additional files e.g. import library . it should be a single html file – Anzu Pang Mar 27 '18 at 08:30
  • I understand. Yet the start time should be in some format. Otherwise how can you convert it to a date? Which is the format you expect? – gil.fernandes Mar 27 '18 at 08:35
  • the expected result is startTime: 09: 30 (user entered value) EndTime: 15:30 (User entered value) the difference would be EndTime - startTime (15:30 - 09:30) then check if the difference exceeded by 24 hr do something – Anzu Pang Mar 27 '18 at 08:39
  • @AnzuPang Please check my updated answer which shows you how to calculate the difference between two times. I have improved error handling also. – gil.fernandes Mar 27 '18 at 09:07
  • Thank you very much for your time and effort, i really appreciate your help – Anzu Pang Mar 29 '18 at 00:16