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 15 of 15
  1. #1
    New Coder
    Join Date
    Jul 2007
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    School day counter

    My school has day 1s and day 2s, which alternate every day. I'm trying to print this with javascript. For example, if monday is a day 1, tuesday a day 2, etc, the following monday would be a day 2 because there are 5 days in a week. Here's what I have so far, but every other week it would be wrong:

    Code:
    <html>
    <body>
    
    <script type="text/javascript">
    today=new Date(); // Initialize Date in raw form
    day = today.getDay(); // Get the day in number form (0,1,2,3,etc.)
    
    var dayName=new Array(7)
    dayName[0]="Sunday";
    dayName[1]="Monday";
    dayName[2]="Tuesday";
    dayName[3]="Wednesday";
    dayName[4]="Thursday";
    dayName[5]="Friday";
    dayName[6]="Saturday";
    
    var dayNumb=new Array(7)
    dayNumb[0]="n/a";
    dayNumb[1]="1";
    dayNumb[2]="2";
    dayNumb[3]="1";
    dayNumb[4]="2";
    dayNumb[5]="1";
    dayNumb[6]="n/a";
    
    document.write (dayName[day]);
    document.write (" is a day ");
    document.write (dayNumb[day]);
    </script>
    
    </body>
    </html>
    Any ideas?

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,065
    Thanks
    36
    Thanked 497 Times in 491 Posts

    Lightbulb Consider this ...

    Seems overly complicated.
    Try this:
    Code:
    <html>
    <body>
    <script type="text/javascript">
    // From: http://codingforums.com/showthread.php?t=164926
    
    var today=new Date(); // Initialize Date in raw form
    var day = today.getDay(); // Get the day in number form (0,1,2,3,etc.)
    
    var dayName = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
    
    var dayNum = '';
    var str = '';
        str += 'From today: (ignoring weekends)<p>';
    for (var i=0; i<14; i++) {
      dayNum = (day+i);
      if ((dayNum % 7 == 0) || (dayNum % 7 == 6)) { str += '<br>'; // bypass
      } else {
        str += dayName[dayNum % 7];
        str += " is a day ";
        str += (dayNum % 2)+1;
        str += '</br>';
      }
    }
    document.write(str);
    
    </script>
    </body>
    </html>

  • #3
    New Coder
    Join Date
    Jul 2007
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Looks good but does it start over every week? I need it to continue from the former week and have a reset method for things such as snow days if possible.

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,065
    Thanks
    36
    Thanked 497 Times in 491 Posts

    Question

    Change this line
    for (var i=0; i<14; i++) {
    to
    for (var i=0; i<28; i++) {
    to go for a 4 week display or some multiple of 7 for the number of weeks to display.

    It does repeat based upon the day of the week.

    I don't know anything about 'snow days' ... I live in Florida.
    What do you mean by 'reset method'?
    Could you explain what you want to happen if it ever snows here?

  • #5
    New Coder
    Join Date
    Jul 2007
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Smile

    I just want to be able to account for random occurances where we don't have school. For example if Tuesday is a day 1, and we don't have school Wednesday, Thursday would continue the count with a day 2 not a 1 like the counter would display. We just skip over days we don't have school. Is there a way I could enter a URL or something to reset this? Something like reset.php?day=2

  • #6
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,065
    Thanks
    36
    Thanked 497 Times in 491 Posts

    Arrow

    I'm still not sure I understand how to determine a random occurrence.
    Are you asking to set the even/odd (1/2) value for a particular day and then continue from there with the display?

    I guess you could change this line only to alter the display from the current 'dayNum'
    from: str += (dayNum % 2)+1;
    to: str += ((dayNum+1) % 2)+1;

    Since you only have two choices (1/2), then you would change it when the 'random' occurrence occurred.

    Only other suggestions would be:

    1. to pass a parameter to the program like:
    SchoolDays.html?daySetting=1
    when it snowed and then
    SchoolDays.html?daySetting=0
    or: SchoolDays.html
    when you wanted to go back into the original sequence
    using the 'daySetting' as the adjustment value for the str = ((dayNum+daySetting) % 2) +1;

    or

    2. read from a .php programs as you proposed, but I don't know anything about php programs. I would not be much of a help here.

    or

    3. read a variable from a file with an AJAX function. Use that variable as in suggestion #1.

    Bottom line: I know it can be done, I just don't know how you want to go about it and how often it would be required.
    If it only changes once or twice (randomly) per year, then I would just modify the script directly in the one line.
    If it happens on a more frequent basis, then passing a parameter might be the better route.

    If you need more than two selections (1/2), you could also do that with the script
    (up to 1-7 times before it cycled back to the original),
    but you would have the same problems with your random occurrences.

  • #7
    New Coder
    Join Date
    Jul 2007
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I'm thinking passing a paramater with the reset.php?day=1 method. I how do I execute the reset though? Just load up the page with the javascript code and add the "?day=1"? This would probably be required once or more per month and not by me, by non technical people helping me which is why I don't want to make direct changes to the code. Thanks for the help!

  • #8
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,065
    Thanks
    36
    Thanked 497 Times in 491 Posts

    Arrow How about this revision?

    See what you think of this modification:
    Code:
    <html>
    <body>
    <script type="text/javascript">
    // From: http://codingforums.com/showthread.php?t=164926
    
    function GetParam(name)
    {
      var start=location.search.indexOf("?"+name+"=");
      if (start<0) start=location.search.indexOf("&"+name+"=");
      if (start<0) return '';
      start += name.length+2;
      var end=location.search.indexOf("&",start)-1;
      if (end<0) end=location.search.length;
      var result='';
      for(var i=start;i<=end;i++) {
        var c=location.search.charAt(i);
        result=result+(c=='+'?' ':c);
      }
      return unescape(result);
    }
    
    function AssignmentSetup() {
      var today=new Date(); // Initialize Date in raw form
      var day = today.getDay(); // Get the day in number form (0,1,2,3,etc.)
    
      var dayName = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
    
      var dayNum = '';
      var dayOffset = 0;
      if (GetParam('dayOffset') != '') { dayOffset = 1; }
    
      var str = '';
          str += 'Call this program with offset of: <br>';
          str += '<a href="DayAssignments.html">DayAssignments.html</a>';
          str += ' or ';
          str += '<a href="DayAssignments.html?dayOffset=1">DayAssignments.html?dayOffset=1</a>';
    
          str += '<p>From today: (ignoring weekends)<p>';
      for (var i=0; i<=21; i++) {
        dayNum = (day+i);
        if ((dayNum % 7 == 0) || (dayNum % 7 == 6)) { str += '<br>'; // bypass
        } else {
          str += dayName[dayNum % 7];
          str += " is a day ";
          str += ((dayNum+dayOffset) % 2)+1;
          str += '</br>';
        }
      }
      return str;
    }
    document.write(AssignmentSetup());
    
    </script>
    
    </body>
    </html>

  • #9
    Banned
    Join Date
    Mar 2009
    Posts
    248
    Thanks
    3
    Thanked 68 Times in 66 Posts
    Deleted as "a bit overcomplicated."
    Last edited by 12 Pack Mack; 04-29-2009 at 10:09 PM.

  • #10
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,065
    Thanks
    36
    Thanked 497 Times in 491 Posts

    Arrow

    Quote Originally Posted by 12 Pack Mack View Post
    NoJo:

    Here's another approach that uses cookies. You'll need to make sure that whatever browser you are using is configured to accept persistent cookies.

    Copy everything below, and using Notepad, save it as an .html file.
    I may be wrong, but I think that might only work on one computer.
    Even my version may be plagued with that problem.

    Ultimate solution may still be based on a server-side file to evaluate
    to determine what to do for ALL computers that call the script.
    (Hence the .php or AJAX solution recommendations)

  • #11
    New Coder
    Join Date
    Jul 2007
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Both of these are right on track. I don't need future dates as much though, just the current date's number and maybe the next.

    jmrker: how can I reset it for either day 1 or day 2 by calling "?day=1" or "?day=2"? This is going to be public so I don't want it resetting whenever someone gets the page without the extension.

    12 Pack: This looks good but I'm thinking of implementing only the "today is a day 1/2", not the form to change the dates (unless that's on a separate page which could get complicated) because this would allow anyone to change it that viewed the page. Also, I'm not understanding totally how to set a new start date. The layout looks very nice though.

    Bottom line: All I really need is a simple script showing what today and tomorrow is (or monday if applicable). I also need a reset method not visible to the public, like something I can enter into the address bar in jmrker's script. Both of these look good so far, but perhaps a bit overcomplicated. I don't need it to be too fancy as I am incorporating them into an already complete layout. I need it to show the same data on all computers loading it, so I am fine with any other server side methods if anyone feels super ambitious.

    Thanks for the help so far!

  • #12
    Banned
    Join Date
    Mar 2009
    Posts
    248
    Thanks
    3
    Thanked 68 Times in 66 Posts
    Just enter a date, like: 3/12/2009 in the top box. Enter whether it is a 1 or 2 in the bottom box. Click the button.

    Never mind. I'll just delete the code.

  • #13
    New Coder
    Join Date
    Jul 2007
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    It may end up being helpful. I'll play around with it.

  • #14
    New Coder
    Join Date
    Jul 2007
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I just thought about this more and I don't really see any feasable way this could work client side. It's back to the drawing board. No one's going to see the same data, and the whole point of this "widget" is to show what day it is regardless of the user's prior knowledge. Having them "set" a day may be helpful if they know, and then they can use it as a reference later (assuming they don't clear their cookies), but it kind of seems to defeat the purpose.

    Thanks for your help guys. I'll implement these for now and try to figure out a php method I suppose that doesn't rely on the client's computer.

  • #15
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,065
    Thanks
    36
    Thanked 497 Times in 491 Posts

    Smile

    Quote Originally Posted by nojo191 View Post
    I just thought about this more and I don't really see any feasable way this could work client side. It's back to the drawing board. No one's going to see the same data, and the whole point of this "widget" is to show what day it is regardless of the user's prior knowledge. Having them "set" a day may be helpful if they know, and then they can use it as a reference later (assuming they don't clear their cookies), but it kind of seems to defeat the purpose.

    Thanks for your help guys. I'll implement these for now and try to figure out a php method I suppose that doesn't rely on the client's computer.
    Which was was I was trying to explain in my feeble attempt in post #10.

    Hope you have some good luck!


  •  

    Tags for this Thread

    Posting Permissions

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