SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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



    Code JavaScript:
    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

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2009
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:


    Code JavaScript:
    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");
    }
    --------------------------
    Karsten Rieger
    http://www.tritium-design.de

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That looks right, thank you
    just monday.. is closed and stated as open on line9

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

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2009
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry, I have corrected the error in the script above.
    On sunday (0), monday (1) and saturday (6) "closed" will always be written.
    --------------------------
    Karsten Rieger
    http://www.tritium-design.de

  5. #5
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah, i'm going to use noscript and redirect them to another page.

  7. #7
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks fidel karsto, i'll be sure to test the script and let you know how it works.

  8. #8
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, i've just tested and it wont show for the open document.write the script just appears blank.

  9. #9
    SitePoint Enthusiast
    Join Date
    Aug 2009
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry I should double check my post. ;-)
    This one I tested (there was a reference to t_hours rather than t_hour):

    Code JavaScript:
            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");
    	}
    --------------------------
    Karsten Rieger
    http://www.tritium-design.de

  10. #10
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll test it now and get back to you, thanks

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

    Thanks for the help

  11. #11
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  12. #12
    SitePoint Enthusiast
    Join Date
    Aug 2009
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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)
    --------------------------
    Karsten Rieger
    http://www.tritium-design.de

  13. #13
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 >= 9 && t_hour <= 15
    || t_hour == 15 && t_min <= 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

  14. #14
    SitePoint Enthusiast
    Join Date
    Aug 2009
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. :-)
    --------------------------
    Karsten Rieger
    http://www.tritium-design.de

  15. #15
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *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)) {

  16. #16
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fidel karsto View Post
    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. :-)
    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

  17. #17
    SitePoint Enthusiast
    Join Date
    Aug 2009
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

    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 :-)
    --------------------------
    Karsten Rieger
    http://www.tritium-design.de

  18. #18
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have tested it now for all the days, it seems to be working for the hours and minutes now

    Thanks for the help : ]

    Code JavaScript:
    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');
        }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •