Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-27-2009, 09:10 PM   PM User | #1
nojo191
New Coder

 
Join Date: Jul 2007
Location: Kansas
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
nojo191 is an unknown quantity at this point
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?
__________________
~Nojo191
Noah Benham's website
nojo191 is offline   Reply With Quote
Old 04-27-2009, 09:38 PM   PM User | #2
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,765
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
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>
jmrker is offline   Reply With Quote
Old 04-28-2009, 12:00 AM   PM User | #3
nojo191
New Coder

 
Join Date: Jul 2007
Location: Kansas
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
nojo191 is an unknown quantity at this point
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.
__________________
~Nojo191
Noah Benham's website
nojo191 is offline   Reply With Quote
Old 04-28-2009, 05:25 AM   PM User | #4
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,765
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
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?
jmrker is offline   Reply With Quote
Old 04-28-2009, 01:06 PM   PM User | #5
nojo191
New Coder

 
Join Date: Jul 2007
Location: Kansas
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
nojo191 is an unknown quantity at this point
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
__________________
~Nojo191
Noah Benham's website
nojo191 is offline   Reply With Quote
Old 04-28-2009, 03:46 PM   PM User | #6
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,765
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
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.
jmrker is offline   Reply With Quote
Old 04-28-2009, 06:14 PM   PM User | #7
nojo191
New Coder

 
Join Date: Jul 2007
Location: Kansas
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
nojo191 is an unknown quantity at this point
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!
__________________
~Nojo191
Noah Benham's website
nojo191 is offline   Reply With Quote
Old 04-29-2009, 03:43 AM   PM User | #8
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,765
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
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>
jmrker is offline   Reply With Quote
Old 04-29-2009, 12:39 PM   PM User | #9
12 Pack Mack
Banned

 
Join Date: Mar 2009
Posts: 248
Thanks: 3
Thanked 68 Times in 66 Posts
12 Pack Mack is an unknown quantity at this point
Deleted as "a bit overcomplicated."

Last edited by 12 Pack Mack; 04-29-2009 at 10:09 PM..
12 Pack Mack is offline   Reply With Quote
Old 04-29-2009, 02:49 PM   PM User | #10
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,765
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
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)
jmrker is offline   Reply With Quote
Old 04-29-2009, 09:55 PM   PM User | #11
nojo191
New Coder

 
Join Date: Jul 2007
Location: Kansas
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
nojo191 is an unknown quantity at this point
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!
__________________
~Nojo191
Noah Benham's website
nojo191 is offline   Reply With Quote
Old 04-29-2009, 10:08 PM   PM User | #12
12 Pack Mack
Banned

 
Join Date: Mar 2009
Posts: 248
Thanks: 3
Thanked 68 Times in 66 Posts
12 Pack Mack is an unknown quantity at this point
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.
12 Pack Mack is offline   Reply With Quote
Old 04-29-2009, 11:34 PM   PM User | #13
nojo191
New Coder

 
Join Date: Jul 2007
Location: Kansas
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
nojo191 is an unknown quantity at this point
It may end up being helpful. I'll play around with it.
__________________
~Nojo191
Noah Benham's website
nojo191 is offline   Reply With Quote
Old 04-30-2009, 12:07 AM   PM User | #14
nojo191
New Coder

 
Join Date: Jul 2007
Location: Kansas
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
nojo191 is an unknown quantity at this point
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.
__________________
~Nojo191
Noah Benham's website
nojo191 is offline   Reply With Quote
Old 04-30-2009, 01:49 AM   PM User | #15
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,765
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
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!
jmrker is offline   Reply With Quote
Reply

Bookmarks

Tags
counter, day 1, day 2, reset script, school

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:53 AM.


Advertisement
Log in to turn off these ads.