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

kranthi picture kranthi · Jan 31, 2012 · Viewed 16.1k times · Source

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

Answer

jabclab picture jabclab · Jan 31, 2012

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);
}