Question

I am very new to javascript and would appreciate some help in understanding this problem.

I am trying to write a function that that checks if the current hour is between hourOne and hourTwo, this is what I have so far:

function checkHourBetween (hourOne, hourTwo)
{
var date = new Date();
var currentHour = date.getHours();

if (currentHour >= hourOne && currentHour <= hourTwo)
    {
        return true;
    }

return false;
}

And I am trying to call it using:

if(checkHourBetween(12, 18))
{
//action
}

I have tested this with a simple document.write("Hello World") inside the if statement just to see if it was working but am getting nothing. My current time is within the specified hours.

I am sure there are better ways to do this (other than javascript) but would love to know where I am going wrong with what I have written.

Was it helpful?

Solution

Nothing wrong with the code as far as I can see, maybe somewhere else in your code is something wrong. Try to run it in FireFox with firebug or with Chrome (no plugin needed) press F12 and check out the console.

function checkHourBetween (hourOne, hourTwo)
{
var date = new Date();
var currentHour = date.getHours();
console.log("currenthour is:",currentHour);
console.log("hourOne is:",hourOne);
console.log("hourTwo is:",hourTwo);
if (currentHour >= hourOne && currentHour <= hourTwo)
    {
        console.log("returning true");
        return true;
    }
console.log("returning false");
return false;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top