Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## mawha

I have this code on our high school website that displays the day of the cycle, for example "Today is day 3". On the weekends it will say "Monday is day 4". We are changing from an 8 day cycle to a 4 day cycle. What do I need to edit on the code below to switch it from an 8 day to a 4 day cycle? Thanks!

function cycleday(last, day){
day = day-1;
//Set the two dates
var lday = last;
today=new Date();
//Get 1 day in milliseconds
var one_day=1000*60*60*24;
//how far from the end of the week that day was
offset = lday.getDay();
//get how many weekends since then
ends = Math.ceil((today.getTime()-lday.getTime())/(one_day));
ends += offset;
ends = Math.floor(ends/7);

//Calculate difference btw the two dates, and convert to days
d = Math.ceil((today.getTime()-lday.getTime())/(one_day));
//subtract the weekends
d = d + day - 1 - ends*2;
//how many days of the cycle
d = d%4;

if (today.getDay() == 0 || today.getDay() == 6){
d += 2;
return "Monday is Day " + d;}
else{
d += 1;
return "Today is Day " + d;}
}

function cycle(){
//update if the counter is off
var lastday = new Date(2010, 4-1, 16); //yyyy, mm-1, dd
var thatday = 2; //the day of the cycle it was
document.write("<span class='cycle'>");
document.write(cycleday(lastday, thatday));
document.write("</div><br><br>");
}

• Originally Posted by kmaw
What do I need to edit on the code below to switch it from an 8 day to a 4 day cycle? Thanks!
Did you ask the person who wrote the code and if so, what did they say?

• it was a former student who am no longer in contact with.

• Originally Posted by kmaw
I have this code on our high school website that displays the day of the cycle, for example "Today is day 3". On the weekends it will say "Monday is day 4". We are changing from an 8 day cycle to a 4 day cycle. What do I need to edit on the code below to switch it from an 8 day to a 4 day cycle? Thanks!
Can you expand on the description of the change?

For example, if you start with "Monday is day 4", will the next day Tuesday be day 1 and Wednesday be day 2 and Thursday is day 3 and Friday then be day 4?
Will that make the following Monday as day 1 in that particular cycle.

What date (not day) is the start of a cycle? Is this always the same year after year or does it change from semester to semester?

• Yes that is correct it just runs in a continuous cycle from day 1 to 4 just as you wrote. At times with storms days and holidays I would just go in and adjust the line near the bottom to reset the day if it was out of sync.

here is the part for the reset...

//update if the counter is off
var lastday = new Date(2010, 4-1, 16); //yyyy, mm-1, dd
var thatday = 2; //the day of the cycle it was
document.write("<span class='cycle'>");
document.write(cycleday(lastday, thatday));
document.write("</div><br><br>");
}

Try out this version and see how close I have come to the requirements.
Note, for testing purposes I have modified the start date to Aug 1, 2011 as cycle 1.
Hopefully it is a bit easier to comprehend than the previous code.

Code:
```<!DOCTYPE HTML>
<html>
<title> Untitled </title>
<style type="text/css">
.cycle { font-family:monospace; }
</style>

<body>
<script type="text/javascript">
function cycleday(last, d, today){
if (today < last) { alert('Invalid start - end dates'); return; }
var str = '';
var tmp = '';
while (last <= today) {
switch (last.getDay()) {
case 0 : d += 0; str = "Monday is Day "; tmp = ((d+1)%4); break;  // Sun
case 6 : d += 0; str = "Monday is Day "; tmp = ((d+1)%4); break;  // Sat
default : d += 1; str = "Today is Day ";  tmp = (d%4);   break;  // Mon-Fri
}
last = new Date(last.getFullYear(),last.getMonth(),last.getDate()+1);
}
if (tmp == 0) { tmp = 4; }
return str+tmp;
}
function cycle(){
//update if the counter is off
var lastday = new Date(2011, 8-1, 1); //yyyy, mm-1, dd
var thatday = 0; // the day of the cycle it was (0-3 for 1-4 display)
var today = new Date(2011,8-1,1);
// only command needed
document.write(cycleday(lastday, thatday, new Date()));

// following for testing purposes
document.write('<p>Test for Month of August 2011<br>');  for (var i=0; i<31; i++) {
tempdate = new Date(today.getFullYear(),today.getMonth(),(today.getDate()+i));
document.write("<span class='cycle'>");
document.write(tempdate.toDateString()+' : '+cycleday(lastday, thatday, tempdate));
document.write("</div><br>");
}
}
cycle();
</script>
</body>
</html>```

• Wow that's great, I'm sure your script will do the trick. I am having some trouble getting it positioned on the site however. Here is the website with the old 8 day cycle. In the index page the line

<script src="day.js" type="text/javascript"></script> calls on the day.js file that I had posted yesterday.You can see it here.

http://www.cec.ccrsb.ca/

--------------------------------------------------------------------

Here is the page with your script
http://www.cec.ccrsb.ca/index4day.html

In this case the line<script src="4day.js" type="text/javascript"></script>calls
on the code that you wrote which I have saved in a js file.

The script seems to be causing some problems. Do you see anything obvious that I am doing wrong, Should I just be swapping out the code in the js file like that?

thanks so much!

• Actually the script is not causing any problems, I just need to figure out how to correct the formatting to make the text white and on its own line as it was in the 8 day cycle.

• Originally Posted by kmaw
Actually the script is not causing any problems, I just need to figure out how to correct the formatting to make the text white and on its own line as it was in the 8 day cycle.
Change the style of the "cycle" display
Code:
```<style type="text/css">
.cycle { font-family:monospace; }
</style>```
You can change the font-family, or leave it as default if you delete it.
You can change the color with color:white;
There are a lot more things you can do with the CSS style, including positioning, backgrounds, emphasis, etc.
You should not need to change the JS at all if it is working correctly for you.

• ## thanks jmrker

It's all working now. Thanks so much for taking the time to share your knowledge with me. I really appreciate your help!

• Originally Posted by kmaw
It's all working now. Thanks so much for taking the time to share your knowledge with me. I really appreciate your help!
You're most welcome.
Happy to help.
Good Luck!

PS: It looks like you forgot your last </span> tag write in your external file.
Code:
```function cycle(){
//update if the counter is off
var lastday = new Date(2011, 8-1, 1); //yyyy, mm-1, dd
var thatday = 0; // the day of the cycle it was (0-3 for 1-4 display)
var today = new Date(2011,8-1,1);
// only command needed
document.write("<span class='cycle'>");
document.write(cycleday(lastday, thatday, new Date()));
document.write('</span>');
}```

• good eye! thanks. I have added it.

•