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');
}
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 objectsDate
for comparison.– DontVoteMeDown
@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 thata
is greater thanb
)!– Wallace Maxters
Right, I meant the second attempt
'09:00' > '8:00'
, misconstrued.– DontVoteMeDown