Web Analytics Made Easy -
StatCounter Flaw in calendar script - CodingForum

Announcement

Collapse
No announcement yet.

Flaw in calendar script

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Flaw in calendar script

    I have adapted the following script (which resides in a .js file) which originally showed calendars for LAST MONTH..THIS MONTH..NEXT MONTH to show instead THIS MONTH..NEXT MONTH..MONTH AFTER THAT.

    It works fine at present but I have not been able to make it work properly at the end of a year (current month = November or December).

    Could some kind person suggest how to modify it to overcome this flaw. NEXT MONTH is fine, but the trouble comes with MONTH AFTER THAT when that month is in next year.


    var flg = 0;
    var fs = 1;
    var bg = "#C0FFFF";
    M = new Array("January","February",
    "March","April","May","June",
    "July","August","September",
    "October","November","December");
    D = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");

    function getBgn() {
    pdy = new Date(); // today
    pmo = pdy.getMonth(); // present month
    pyr = pdy.getYear(); // present year
    if (pyr < 2000)
    pyr = pyr + 1900;
    yr = yr1 = (pmo==0?pyr-1yr); // last month's year
    mo = (pmo==0?11mo-1); // last month
    bgn = new Date(M[mo]+" 1,"+yr1); // assign to date
    document.write('<TABLE BORDER=0><TR><TD VALIGN=TOP>');

    document.write('</TD><TD VALIGN=TOP>');
    yr = pyr; // present year
    mo = pmo; // present month
    bgn = new Date(M[mo]+" 1,"+yr); // assign to date
    Calendar(); // Send this month to screen

    document.write('</TD><TD VALIGN=TOP>');
    yr = (pmo==11?pyr+1yr); // next month's year
    mo = (pmo==11?0mo+1); // next month
    bgn = new Date(M[mo]+" 1,"+yr); // assign to date
    Calendar(); // Send next month to screen

    document.write('</TD><TD VALIGN=TOP>');
    yr = (pmo==11?pyr+2yr); // next month's year
    mo = (pmo==11?0mo+2); // next month
    bgn = new Date(M[mo]+" 1,"+yr); // assign to date
    Calendar(); // Send third month to screen

    document.write('</TD></TR></TABLE>'); // Finish up
    }
    function Calendar(){
    dy = bgn.getDay();
    yr = eval(yr);
    d = "312831303130313130313031";
    if (yr / 4 == Math.floor(yr / 4)) {
    d = d.substring(0, 2) + "29" + d.substring(4, d.length);
    }
    pos = (mo * 2);
    ld = eval(d.substring(pos, pos + 2));
    document.write("<TABLE BORDER=1"
    + " BGCOLOR='" + bg
    + "'><TR><TD ALIGN=CENTER COLSPAN=7>"
    + "<FONT SIZE=" + fs + ">" + M[mo] + " " + yr
    + "</FONT></TD></TR><TR><TR>");
    for (var i = 0;i < 7;i ++) {
    document.write("<TD ALIGN=CENTER>"
    +"<FONT SIZE=1>" + D[i] + "</FONT></TD>");
    }
    document.write("</TR><TR>");
    ctr = 0;
    for (var i = 0;i < 7; i++){
    if (i < dy) {
    document.write("<TD ALIGN=CENTER>"
    +"<FONT SIZE=" + fs + "> </FONT>"
    +"</TD>");
    }
    else {
    ctr++;
    document.write("<TD ALIGN=CENTER>"
    + "<FONT SIZE=" + fs + ">" + ctr + "</FONT>"
    + "</TD>");
    }
    }
    document.write("</TR><TR>");
    while (ctr < ld) {
    for (var i = 0;i < 7; i++){
    ctr++;
    if (ctr > ld){
    document.write("<TD ALIGN=CENTER>"
    + " </TD>");
    }
    else {
    document.write("<TD ALIGN=CENTER>"
    + "<FONT SIZE=" + fs + ">" + ctr + "</FONT>"
    + "</TD>");
    }
    }
    document.write("</TR><TR>");
    }
    document.write("</TR></TABLE>");}

    =============================================


    <SCRIPT language=\"JavaScript\">getBgn();</SCRIPT>


    Any assistance much appreciated!

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #2
    I came across a similar problem when I was programming my own calendar script.

    The problem lies with this: Mathematically, the number after 12 (December) is 13. You need to do a check for the current year, and as the month goes over the 12 mark into 13, you need to add a year, then subtract 12 months. You'd use the reverse when you're going backwards from January to December.

    Getting those months/years to rollover is a bit of a pain.

    Comment


    • #3
      Thanks Skyzyx!

      I have fixed it now.

      If anyone is interested, the following changes make the third month appear OK after ayear end roll-over:-

      document.write('</TD><TD VALIGN=TOP>');
      yr = (pmo==10?pyr+1yr); // next month's year
      if (pmo==11) {yr=pyr+1}
      mo = (pmo==10?0mo+2); // next month is January
      mo = (pmo==11?1mo+2); // next month is February
      if (mo>11) {mo=mo-12}
      bgn = new Date(M[mo]+" 1,"+yr); // assign to date
      Calendar(); // Send third month to screen

      All the code given in this post has been tested and is intended to address the question asked.
      Unless stated otherwise it is not just a demonstration.

      Comment

      Working...
      X