Search code examples
javascriptjquerydatetimeweekday

How to check if current time falls within a specific range on a week day using jquery/javascript?


When a user visits a page I need to check if the current time falls between 9:00 am and 5:30pm on a weekday and display something using jquery/javascript.But I am not sure how can check that.

Could someone please help?

Thanks


Solution

  • This should hopefully help:

    function checkTime() {
        var d = new Date(); // current time
        var hours = d.getHours();
        var mins = d.getMinutes();
        var day = d.getDay();
    
        return day >= 1
            && day <= 5
            && hours >= 9 
            && (hours < 17 || hours === 17 && mins <= 30);
    }