Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Dec 2003
    Location
    UK
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Scheduler script - getHours complication

    Hi all, a colleague and I have been trying to fathom whether there is a solution to this problem, and we'd very much appreciate any help we can get on the matter!

    We're running a scheduler script as below:

    today = new Date()
    day = today.getDay()
    hr = today.getHours()

    if (day ==1)
    {if ((hr ==0) || (hr ==1) || (hr ==2) || (hr ==3) || (hr ==4) || (hr ==5) || (hr ==6) || (hr ==7) || (hr ==8) || (hr ==9) || (hr ==10) || (hr ==11) || (hr ==12) || (hr ==13) || (hr ==14) || (hr ==15) || (hr ==16))
    document.write("a")
    if ((hr ==17) || (hr ==18))
    document.write("b")
    if ((hr ==19) || (hr ==20))
    document.write("c")
    if ((hr ==21) || (hr ==22) || (hr ==23))
    document.write("d")}

    if (day ==2) etc. etc.

    ...which is fine if the schedule changes on the hour. Is it at all possible to alter the script so that if the schedule changes on the half hour, the content of the script can as well, as in, for example, at 17:30, so you don't have to wait until 18:00 for the script to change, or have it running early from 17:00?

  • #2
    Regular Coder
    Join Date
    Dec 2003
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Use minutes. min = today.getMinutes()

  • #3
    New Coder
    Join Date
    Dec 2003
    Location
    UK
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Right, for it to change at 17:30 then, would it work like this?

    today = new Date()
    day = today.getDay()
    hr = today.getHours()
    min = today.getMinutes()

    if (day ==1)
    {if ((hr ==0) || (hr ==1) || (hr ==2) || (hr ==3) || (hr ==4) || (hr ==5) || (hr ==6) || (hr ==7) || (hr ==8) || (hr ==9) || (hr ==10) || (hr ==11) || (hr ==12) || (hr ==13) || (hr ==14) || (hr ==15) || (hr ==16) || (hr ==17)
    document.write("a")
    if ((hr ==17) || (min ==30) || (hr ==18))
    document.write("b")
    if ((hr ==19) || (hr ==20))
    document.write("c")
    if ((hr ==21) || (hr ==22) || (hr ==23))
    document.write("d")}

    if (day ==2) etc. etc.

  • #4
    me'
    me' is offline
    Senior Coder
    Join Date
    Nov 2002
    Location
    Warwickshire, England
    Posts
    1,229
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well first off your script could be a lot shorter:
    Code:
    today = new Date()
    day = today.getDay()
    hr = today.getHours()
    min = today.getMinutes()
    
    switch (today) {
      case 1: (hr <= 16) ? document.write('a') : 
        (hr >= 17 && hr <= 18) ? document.write('b') : 
        (hr >= 19 && hr <= 20) ? document.write('c') : 
        (hr >= 21 && hr <= 23) ? document.write('d') : return false;
      case 2: //etc.}
    I'll have a think about your question.
    David House - Perfect is achieved, not when there is nothing left to add, but when there is nothing left to take away. (Antoine de St. Exupery).
    W3Schools | XHTML Validator | CSS Validator | Colours | Typography | HTML&CSS FAQ | Go get Mozilla Now | I blog!

  • #5
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Change all of the numbers in "me'"s code to be hour and ahalf

    aka.... 17 would be 17.5

    then you can use this code.

    min = today.getMinutes()
    if(min >= 30)hr +=.5;

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #6
    New Coder
    Join Date
    Dec 2003
    Location
    UK
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for all your help so far, still can't get it to work though....

    This is how I interpreted that code (case 1 being Monday, case 7 Sunday etc.):

    today = new Date()
    day = today.getDay()
    hr = today.getHours()
    min = today.getMinutes()
    if(min >= 30)hr +=.5;

    switch (today) {
    case 1: (hr <= 23) ? document.write('a') : return false;
    case 2: (hr <= 23) ? document.write('a') : return false;
    case 3: (hr <= 23) ? document.write('a') : return false;
    case 4: (hr <= 23) ? document.write('a') : return false;
    case 5: (hr <= 23) ? document.write('a') : return false;
    case 6: (hr <= 23) ? document.write('a') : return false;
    case 7: (hr <= 14) ? document.write('a') : return false;
    (hr >= 15.5 && hr <= 16) ? document.write('b') :
    (hr >= 17 && hr <= 18) ? document.write('c') :
    (hr >= 19 && hr <= 20) ? document.write('d') :
    (hr >= 21 && hr <= 23) ? document.write('e') : return false;

    Tried using it, and it displayed nothing at all - where am I going wrong?

  • #7
    New Coder
    Join Date
    Dec 2003
    Location
    UK
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your help, guys - we sorted the problem.


  •  

    Posting Permissions

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