Skip to content Skip to sidebar Skip to footer

Between 11pm And 7am In Javascript

What's the best way to do a simple return function to check if it's between 11pm and 7am? Had something like this in mind, but the problem with this is that 1am would be 1 and that

Solution 1:

Pretty close, but you should be using || not &&

var currentHour = (newDate()).getHours();
return (currentHour >= 23) || (currentHour <= 7);

If the hours are greater or equal to 23 (11pm) OR if hours is less than or equal to 7 (7am), then true it's between 11pm and 7am.

Noon, getHours() == 12, so then 12 >= 23 = false || 12 <= 7 = false, therefore return false;

Post a Comment for "Between 11pm And 7am In Javascript"