Val Kalinic Val Kalinic - 5 months ago 16
HTML Question

Using JavaScript comparison operators with current time

I am making an HTML table that should hide certain parts according to the time using JavaScript, for example;


6:30

6:45

7:05

When the current time is equal or greater than 6:30 the first cell should hide.


The way I start this is;


var now = new Date(); // to create date object

var h = now.getHours(); // to get current hour

var m = now.getMinutes(); // to get current minute


And then later;


if (h>=6 && m>=30) {

$('table#truetable tr:first').hide();

}


This does not work (I think the problem is in the last part), as it wouldn't hide this (first) cell in let's say 7:25 as the minute number then isn't greater than 30, which means this way wouldn't work in many other cases.

Can I fix this? Do I need to do it another way?

Answer

Compare by minutes:

if( h*60+m/*h:m*/ >= 6*60+30/*6:30*/ ){
}