jQuery-Student1989
jQuery-Student1989

Reputation: 85

Displaying Open or Closed Status based on Time and Day

What is the easiest way to display "We are Open/Closed" on a web page based on the day and time it is.

I found the code here, but it doesn't take into consideration time zones. How do I modify the script to display the open/closed status based on timezone?

What I have so far.

<span id="hoursofoperation"></span>
<script>
    var a = document.getElementById("hoursofoperation");
    var d = new Date();
    var n = d.getDay();
    var now = d.getHours() + "." + d.getMinutes();
    var weekdays = [
        ["Sunday", 9.30, 12.00, 15.30,22.00],
        ["Monday", 8.30, 12.00, 15.30,19.00],
        ["Tuesday", 8.30, 12.00, 15.30,19.00],
        ["Wednesday", 8.30, 12.00, 15.30,19.00],
        ["Thursday", 8.30, 12.00, 15.30,19.00],
        ["Friday", 8.30, 11.30],
        ["Saturday"] // we are closed, sorry!
    ];
    var day = weekdays[n];


    if (now > day[1] && now < day[2] || now > day[3] && now < day[4]) {
        a.innerHTML = "We are Open.";
    }
     else {
        a.innerHTML = "We are Closed.";
    }


</script>

Upvotes: 1

Views: 124

Answers (1)

php_nub_qq
php_nub_qq

Reputation: 16017

Just use .getUTCHours and .getUTCMinutes instead.

Make sure to also convert your table to UTC times.

Upvotes: 1

Related Questions