Business Hours text according to time and/or day

I must have tried a 100 variations of this code and can't seem to get it right. I feel like I'm walking further off the playing field.

I want to display a message on our pages that show whether our business is open or closed depending on time and/or day.

Here is my current code.

<script type="text/javascript">
var today = new Date()
var open = ("We're open today from 9am - 5pm</span>");
var closed = ("We're closed and will open again tomorrow 9am - 6pm</span>");
if (today == 0) display.innerHTML = 'closed';
if (today.getHours() >= 9 && today.getHours() < 18) {
    display.innerHTML = 'open';
} else {
    display.innerHTML = 'closed';
}
</script>

Open in new window


and the HTML I'm currently using.

<div><span id="display"></span></div>

Open in new window

hrhsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun KlineLead Software EngineerCommented:
Are you defining your display object elsewhere on your page? It is not defined in the snippet of code you provided. Also, you are setting the innerHTML to text, not the variables you have defined. For those two lines of code, you should use something similar to:
display.innerHTML = open;

Open in new window

It is advisable to use more descriptive names for your variables, such as openMessage and closedMessage.

You are also including an end SPAN tag in you message, but it is not needed. innerHTML will put the text between the start and end SPAN tags.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hieloCommented:
You need to update display after it comes into existence.  To do this move your <script> tag after the closing <body> tag OR wrap your code in a function and have it execute upon window load (see below):

<script type="text/javascript">
window.onload=start;

function start(){
var display = document.getElementById("display");
var today = new Date()
var open = ("We're open today from 9am - 5pm");
var closed = ("We're closed and will open again tomorrow 9am - 6pm");
if (today == 0) display.innerHTML = 'closed';
if (today.getHours() >= 9 && today.getHours() < 18) {
    display.innerHTML = 'open';
} else {
    display.innerHTML = 'closed';
}
}
</script>

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.