String comparison (which answers hours) in javascript is reliable? How does it work?

Asked

Viewed 3,472 times

2

It is known that, in terms of time, that 07:30 is less than 08:30 - speaking of hours.

In javascript, if we make this comparison, everything goes as expected above

'07:30' < '08:00' // true

'08:30' > '09:00' // false

But in the following case, the result is not as expected - in terms of hours, or comparison of hours.

'09:00' > '8:00' // false

Knowing that in the two examples above has nothing to do with the question of hours, but some internal behavior of javascript, how this evaluation is done?

I can rely on this time comparison (since all are started with the string 0)?

Stirring of the issue

I need to make a comparison to see if a particular establishment is closed or not.

So I did like this

<tr class='horario'
   data-agora='<?=date('H:i:s')?>'
   data-final="18:00:00"
   data-inicial="08:00:00"
>
</tr>

var horario = $('.horario').data();

if (horario.agora >= horario.inicial && horario.agora <= horario.final) {
    $('#status').html('Aberto');
} else {
  $('#status').html('Fechado');
}
  • 1

    If you saw that case '08:30' > '09:00' gives wrong I think your question as to whether it’s trustworthy or not is already answered, right? The right thing would be to ask how to do it the right way. I would turn into objects Date for comparison.

  • @Dontvotemedown, in fact this comparison you made would give false (and that is correct). Eight and a half is less than Nine o'clock. The point is, when you get zero out of the way, then the comparison is compromised. I wanted to understand how the JS makes this comparison (i.e., how the JS knows that ais greater than b)!

  • Right, I meant the second attempt '09:00' > '8:00', misconstrued.

1 answer

7


I think the best way is to turn the string into an object Date, I find it safer than comparing strings or manipulating them to do so. A very simple way is as follows::

// Verifica se hora1 é maior que hora2.
function compararHora(hora1, hora2)
{
    hora1 = hora1.split(":");
    hora2 = hora2.split(":");

    var d = new Date();
    var data1 = new Date(d.getFullYear(), d.getMonth(), d.getDate(), hora1[0], hora1[1]);
    var data2 = new Date(d.getFullYear(), d.getMonth(), d.getDate(), hora2[0], hora2[1]);

    return data1 > data2;
};

Fiddle

But a suggestion is, if you have other uses related to date and time in your system, use the momentjs.

  • +1 Aff, had used the moments.js, but I took it. I thought I was giving in, but it was my logic that was wrong :(

  • Yeah, I was gonna comment on your if: horario.agora >= horario.final || horario.agora <= horario.final. The comparison would not be with horario.inicial(if ownership exists) and horario.final respectively?

  • But so, if you use Moment just for this operation, I don’t think it pays to have the library. It’s much more fun than that, it’s kind of Overkill.

  • I suffered quite a bit with dates until I found Moment.js. I don’t think it’s Overkill no. It’s light and saves a lot of work.

  • 2

    +1 by nick :D

Browser other questions tagged

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