Schedule shop open&closed times js

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 :frowning:

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

Regards
Whytey

I have tested it now for all the days, it seems to be working for the hours and minutes now

Thanks for the help : ]

var d = new Date();
var t_hour = d.getUTCHours(); // getUTC for GMT0
var t_min = d.getUTCMinutes();
var t_day = d.getUTCDay();
 
    if (t_day == 5 && t_hour >= 11 && t_hour <= 18){
    // it's friday between 12pm and 8pm
        document.write('open');
		
	} else if (t_day == 6 && t_hour >= 7 && t_hour <= 12){
    // it's saturday between 8am and 2pm
        document.write('open');
		
    } else if((t_day == 2 || t_day == 3 || t_day == 4)
				  && (t_hour >= 8 && t_hour <= 15 || t_hour == 16 && t_min <= 30)) {
    // it's tuesday to thursday between 9am and 5:30pm
        document.write('open');
    } else {
		
    // any other time
        document.write('closed');
    }

It displays close now but i will change the script to what you gave me and change my local time settings back to 5:30 see if it writes close

Thanks again


Edit

Ok, i tested it a few times it didnt appear to display closed for 17:30 so i changed to (15) not sure why it only works 2 hours before = /

     && (t_hour &gt;= 9 && t_hour &lt;= 15
          || t_hour == 15 && t_min &lt;= 30)) {

it displays closed on 5:30 and open anything before. does this look right to you? i’ll test it more on all the times etc.

also had to change (9) to (8) -1hour so it displays open for 9am

just change this
(t_hour >= 9 && t_hour <= 17 || t_hour == 17 && t_min <= 30)
to
(t_hour >= 9 && t_hour <= 16 || t_hour == 17 && t_min <= 30)

I’m not quite sure if I understand you correctly.
If you want to display “open” or “closed” depending on the current date, it should work like this:


var d = new Date();
var t_hour = d.getUTCHours(); // getUTC for GMT0
var t_min = d.getUTCMinutes();
var t_day = d.getUTCDay();

if (t_day == 5 && t_hour >= 8 && t_hour <= 14){
// it's friday between 8am and 2pm UTC
    document.write("open");
} else if((t_day == 2 || t_day == 3 || t_day == 4) 
         && (t_hours >= 9 && t_hours <= 17 
          || t_hours == 17 && t_min <= 30)) {
// it's tuesday to thursday between 9am and 5:30pm
    document.write("open");
} else {
// any other time
    document.write("closed");
}

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.

Any idea why its not writing as closed?

I’ll test it now and get back to you, thanks :slight_smile:

edit: it appears to display open at the correct time.

Thanks for the help :slight_smile:

Sorry to confuse you but i think its just +1 because i did more testing and it displayed the correct times for t_hour- 16

nevermind this for now… more importantly is figuring out why the minutes are not applying as i posted adobve

At the moment of this post my timezone is now 21:10 (current time) GMT or 9:10pm

yeah, i’m going to use noscript and redirect them to another page.

Thanks fidel karsto, i’ll be sure to test the script and let you know how it works.

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.

sorry, I have corrected the error in the script above.
On sunday (0), monday (1) and saturday (6) “closed” will always be written.

That looks right, thank you :slight_smile:
just monday… is closed and stated as open on line9

how would i add a document write close for that day?
Thanks

this is a simple mistake in logic I did first too! (otherwise you wouldn’t have run into this) :slight_smile:
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

this in code would be:

[…]
&& (t_hour >= 8 && t_hour <= 15 || t_hour == 16 && t_min <= 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 :slight_smile:

Newest Edit

I have a few problems.

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

+1 hour

t_day == 2 || t_day == 3 || t_day == 4)
&& (t_hour >= 8 && t_hour <= 16
|| t_hour == 16 && t_min <= 30)) {

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. :slight_smile:

I’m sorry I should double check my post. :wink:
This one I tested (there was a reference to t_hours rather than t_hour):


        var d = new Date();
	var t_hour = date.getUTCHours(); // getUTC for GMT0
	var t_min = date.getUTCMinutes();
	var t_day = date.getUTCDay();
	
	if (t_day == 5 && t_hour >= 8 && t_hour <= 14){
	// it's friday between 8am and 2pm UTC
	    document.write("open");
	} else if((t_day == 2 || t_day == 3 || t_day == 4) 
	         && (t_hour >= 9 && t_hour <= 17 
	          || t_hour == 17 && t_min <= 30)) {
	// it's tuesday to thursday between 9am and 5:30pm
		document.write("open");
	} else {
	// any other time
		document.write("closed");
	}

Hi, i’ve just tested and it wont show for the open document.write the script just appears blank.