Warn when time is different from time

Asked

Viewed 109 times

2

Hello, I have two vars storing two times in format minutos:segundos:milisegundos:

var tempo1 = "11:10:10";
var tempo2 = "11:10:10";

I would like to generate a warning when the tempo1 is right (greater or lesser) in 0,5 seconds of the tempo2.

  • 1

    And will you have decimal values in seconds? Can [Edit] the question and put what you have tried to do and write down the difficulty found?

  • I haven’t done anything yet, I’m lost on how to start this comparison, decimal values in the second

  • These 3 time parameters are hours, minutes and seconds? or minutes, seconds, milliseconds?

  • @DVD I didn’t realize how one can detect differences of less than a second if the accuracy degree of the input is 1 second (in case it is hh:mm:ss). Hence the doubt whether the last parameter would be milliseconds...

2 answers

5


If your input is minutos:segundos:milisegundos you may know if the difference between the two is less than 300ms for example like:

function diff300(a, b) {
  a = a.split(':').map(Number);
  b = b.split(':').map(Number);
  return Math.abs(
      new Date(2000, 0, 1, 1, ...a) - new Date(2000, 0, 1, 1, ...b)
  ) > 300;
}

console.log(diff300("11:10:10", "11:10:10")); console.log(diff300("11:10:10", "11:10:510"));

The idea is to create two dates with minutes, seconds and milliseconds as the only difference and then check whether the absolute difference between them is less than 300ms

5

Further down, follow one alternative answer and shortest. And also more economical in resources.

The function takes three arguments, in order:

  • time 1
  • time 2
  • value to be compared in milliseconds (500, in this case)

If you don’t need anything generic (always 500ms), you can remove the third argument. But do not forget to exchange in all places within the function, appropriately, for 500 where there was the name of the third argument.

When converting the times to Date, You need to put false data for time zone and day, month and year. It does not interfere with the result since these extra data are the same for both cases.

The function returns true for cases where the difference is greater or less than the value specified in the 3rd argument. Returns false if it is exactly and equal to the specified value.

The way to deal with the result is to the taste of the customer. If you want to use alert() or other resources, just change.

// Tempos armazenados a serem verificados
var tempo_1 = "13:12:11";
var tempo_2 = "13:12:12";

// Função verificadora de diferença
function dif_ms(tempo_1, tempo_2, comparativo) {

  // Converte os tempos para o tipo `Date` e obtme tempo total em milissegundos
  var t1 = new Date("2000-01-01T" + tempo_1 + "Z").getTime();
  var t2 = new Date("2000-01-01T" + tempo_2 + "Z").getTime();

  // Verifica a diferença
  var delta = Math.abs(t1 - t2);

  // Retorna 'true' se for diferente (maior ou menor) que 500ms ou 0.5s
  return delta != comparativo;

}

// Exemplo de saída
console.log("- É diferente de 500ms (ou 0,5s)? \n- " + (dif_ms(tempo_1, tempo_2, 500) ? "Sim." : "Não."));

  • 2

    Haha, we responded in the same minute with more or less the same idea :) +1

  • 1

    @Sergio LOL! I just saw, haha! high five But I must say, yours is more elegant! ;)

  • How do you put these examples and code execution here in Stack Overflow?

  • 1

    @Branches when asking the question (or answer), will have a button on the right side of the image insertion button. It displays "< >" drawn on it. Clicking will open a "modal" (window imitation) in the Jsfiddle templates. Then just put the code in the appropriate location (CSS, HTML and Javascript).

  • @José Thanks! I will use.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.