I am having problems with this script. I’m trying to get it to display a document write on the website if the time is as specified and the day is.
These are the times and days i want to display for:
Monday: CLOSED
Tuesday: 9AM - 5:30PM
Wednesday: 9AM - 5:30PM
Thursday: 9AM - 5:30PM
Friday: 8AM - 2PM
Sunday: CLOSED
At the moment this script only writes for
Monday to Friday, 8:30am to 6pm (GMT0).
But as you can see from my times above i want to be able to show two unique times for 2 days tues-thurs and friday (open) / sat-sun (close).
i assume the else statement would be used for both monday and sunday as it writes close?
I have no clue how to go about this, tried changing the the t_day but had no success
var d = new Date();
var t_hour = d.getUTCHours(); // getUTC for GMT0
var t_min = d.getUTCMinutes();
var t_day = d.getUTCDay();
if (t_hour == 8 && t_min >= 30 && t_day > 0 && t_day <=5)
{
document.write('open');
}
else if (t_hour > 8 && t_hour < 18 && t_day > 0 && t_day <=5)
{
document.write('open');
}
else
{
document.write('closed.');
}
To declare the script in my html doc i’ve just used
<script src=“js/open.js” type=“text/javascript”></script>
If anyone can help me with getting the times and dates as stated above working that would be greatly appreciated
I’m not sure why but its past 5:30pm now as the the script sets the time to anything later to display closed.
For some reason its still displaying open on the document write. UTC for GMT0 is the correct time zone if i’m not mistaken.
If this is for a “real website” and not a school project then you will probably need a Plan B for those, abeit most likely very few, users who have javascript deliberately or accidentally turned off in their browsers.
this is a simple mistake in logic I did first too! (otherwise you wouldn’t have run into this)
let me try to explain:
in the following statement:
[…]
&& (t_hour >= 8 && t_hour <= 16 || t_hour == 16 && t_min <= 30)) {
[…]
it is checked whether the hours are greater than or equal to 8 and less than or equal to 16 (in other words: between 8 and 16 including 8 and 16) or (this is expressed by “||”)
when hours are 16 then minutes have to be less than or equal to 30.
and that is the bug here: when hours are 16, minutes are not checked anymore, because the precondition is met already - so it will be displayed “open”.
to fix that, you’ll have to change the condition as follows:
first in pseudo code:
display “open” only when
hours are between 8 and 15
or
when hours are 16 then the minutes have to be less than or equal to 30
again - the first check for hours between 8 and 15 does not take minutes into account. all times from 8:00 to 15:59 will be valid for this case. only if time is between 16:00 and 16:30 then the second check (the “or” part) comes into play.
hope this helps you!
looking back to this thread, this is pretty basic stuff, but it seems to be a quite nice beginner tutorial
I don’t think minutes are applying to the script - tried changing my local time to 17:35,45,50 and it was still wrote OPEN until i changed my local time by the hour 18:00
that is because your local timezone is +2 hours after UTC! that is the same time zone as here in germany where I am. therefore when your shop is in a timezone which is +2 hours after UTC you should adapt the hours accordingly.