Jump to content

Recommended Posts

I have a website with a date selection for notes and the date is not reflecting the leap year - is there something in the code I can change to reflect the leap year? After this month all the dates will be off by one if not.  Thanks

 

<script language="JavaScript" type="text/javascript">
<!--
var months = new Array('January', 'February', 'March', 'April', 'May', 'June',
                       'July', 'August', 'September', 'October', 'November', 'December');
var monthDays = new Array(31, '', 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
 
// check url query
var pageQuery = (location.search) ? true : false;
var pageDate = (pageQuery) ? location.search : new Date();
var pageYear = (pageQuery) ? pageDate.substring(1,5) : pageDate.getFullYear();
var pageMonth = (pageQuery) ? pageDate.substring(6, : pageDate.getMonth();
 
// check for leap year
monthDays[1] = ((Number(pageYear)%4 == 0 && Number(pageYear)%100 == 0 ) || (Number(pageYear)%400 == 0)) ? 29 : 28;
               
if (Number(pageMonth) < 10) pageMonth = '0' + Number(pageMonth);
 
var pageDateDay = (pageQuery) ? pageDate.substring(9,10) : pageDate.getDate();
pageDateDay = ( Number(pageDateDay) < 10 ) ? '0' + Number(pageDateDay) : pageDateDay.toString();
if (pageDateDay == 0) pageDateDay = 1;
 
display_window_title: window.document.title = months[Number(pageMonth)] + ' ' + pageYear;
 
function monthDropDown() { // Populates month drop down with the months Array
  var monthSelect = new String();
  
  for (var  i = 0; i < months.length; i++ ) {
    monthSelect += '<option value="' + i + '"';
    if (i == Number(pageMonth)) monthSelect += ' selected';
    monthSelect += '>' + months[i] + '</option>\n';
  }
  document.write(monthSelect);
}
 
function yearArray() { // populate year array of length = 9 for yearDropDown()
  var yearNum = Number(pageYear) - 4;
  var year = new Array();
  
  for ( var i=0; i<9; i++ ) {
    year[i] = yearNum + i;
  }
  
  return year;
}
  
function yearDropDown() { // Populates drop down options for year
  var years = yearArray();
  var yearSelect = new String();
  
  for ( i=0; i<years.length; i++ ) {
    yearSelect += '<option value="' + years[i] + '" ';
    if ( i == 4 ) yearSelect += 'selected';
    yearSelect += '>' + years[i] + '</option>\n';
  }
  
  document.write(yearSelect);
}
 
function changeStyle(tag, funct, wkdy) { // MouseOver and MouseOut functions for day buttons
  if ( funct == 'over' ) {
    if ( wkdy == '1' ) {
      tag.style.backgroundColor = '#FFFFFF';
      tag.style.color = '#000000';
    } else {
      tag.style.backgroundColor = '#E2E2E2';
      tag.style.color = '#CC0000';
    }
    tag.style.borderColor = '#000000';
    tag.style.fontWeight = 'bold';
  } else {
    if ( wkdy == '1' ) {
      tag.style.backgroundColor = '#F2F2F2';
      tag.style.color = '#333333';
    } else {
      tag.style.backgroundColor = '#CCCCCC';
      tag.style.color = '#CC0000';
    }
    tag.style.borderColor = '#666666';
    tag.style.fontWeight = 'normal';
  }
}
 
function monthCalendar() { // Populates month calendar with all days of the month
  var cursor = ( navigator.appName == 'Netscape' ) ? 'pointer' : 'hand'; // Check which browser for cursor style
  var monthString = new String(months[Number(pageMonth)]);
  var dayTitles = new Array('S','M','T','W','T','F','S');
  var days = monthDays[Number(pageMonth)];
  var thisMonth = new Date(monthString + ' ' + '01' + ', ' + pageYear);
  var firstDay = thisMonth.getDay();
  var weekTr = new String();
  pageMonth = Number(pageMonth) + 1;
  var actualMonth = Number(pageMonth) - 1;
  pageMonth = ( pageMonth < 10 ) ? '0' + pageMonth : pageMonth.toString();
  var dayInc = 1;
  var column = 0;
  var weekTitleTr = new String();
  var trs = 1;
  var weekDayStyle = 'style="cursor:' + cursor + ';" onMouseOver="changeStyle(this,\'over\',\'1\');"' +
                     ' onMouseOut="changeStyle(this,\'out\',\'1\');"';
  var weekEndStyle = 'style="background-color: #CCCCCC; color: #CC0000; cursor:' + cursor +
                     ';" onMouseOver="changeStyle(this,\'over\',\'0\');" ' +
                     'onMouseOut="changeStyle(this,\'out\',\'0\');"';
    
  for ( i=0; i<dayTitles.length; i++ ) weekTitleTr += '<td width="34" class="dayTitles">' + dayTitles[i] + '</td>';
  
  for ( i=0; i<firstDay; i++ ) weekTr += '<td width="34">  </td>';
  
  for ( i=firstDay; i<7; i++ ) {
    var dayIncValue = ( dayInc < 10 ) ? '0' + dayInc : dayInc.toString();
    
    if ( i != 6 && i != 0 ) {
      weekTr += '<td width="34"><input name="button" type="button" value="' + dayInc +
                '" width="34" ' + weekDayStyle + ' onClick="javascript:opener.document.forms[0].DATE.value=\'' +
                pageYear + '-' + pageMonth.toString() + '-' + dayIncValue + '\'; window.close();" ' +
                'title="' + months[actualMonth] + ' ' + dayIncValue.toString() + ', ' + pageYear + '"></td>\n';
    } else {
      weekTr += '<td width="34"><input name="button" type="button" value="' + dayInc + 
                '" width="34" ' + weekEndStyle + ' onClick="javascript:opener.document.forms[0].DATE.value=\'' +
                pageYear + '-' + pageMonth.toString() + '-' + dayIncValue + '\'; window.close();" ' +
                'title="' + months[actualMonth] + ' ' + dayIncValue.toString() + ', ' + pageYear + '"></td>\n';
    }
    dayInc++;
  }
  
  for ( i=dayInc; i<(days+1); i++ ) {
    dayIncValue = ( i < 10 ) ? '0' + i : i.toString();
    
    if ( column == 0 || column == 7 ) {
      weekTr += '</tr><tr><td width="34"><input name="button" type="button" value="' +
                i + '" width="34" ' + weekEndStyle + ' onClick="javascript:opener.document.forms[0].DATE.value=\'' +
                pageYear + '-' + pageMonth.toString() + '-' + dayIncValue + '\'; window.close();" ' +
                'title="' + months[actualMonth] + ' ' + dayIncValue.toString() + ', ' + pageYear + '"></td>\n';
      column = 0;
      trs++;
    } else if (column == 6) {
      weekTr += '<td width="34"><input name="button" type="button" value="' + i +
                '" width="34" ' + weekEndStyle + ' onClick="javascript:opener.document.forms[0].DATE.value=\'' +
                pageYear + '-' + pageMonth.toString() + '-' + dayIncValue + '\'; window.close();" ' +
                'title="' + months[actualMonth] + ' ' + dayIncValue.toString() + ', ' + pageYear + '"></td>\n';
    } else {
      weekTr += '<td width="34"><input name="button" type="button" value="' + i + '" width="34" ' +
                weekDayStyle + ' onClick="javascript:opener.document.forms[0].DATE.value=\'' +
                pageYear + '-' + pageMonth.toString() + '-' + dayIncValue + '\'; window.close();" ' +
                'title="' + months[actualMonth] + ' ' + dayIncValue.toString() + ', ' + pageYear + '"></td>\n';
    }               
    column++;
  }
  
  for ( i=column; i<7; i++ ) {
    weekTr += '<td width="34">  </td>';
    column++;
  }
  
  while ( trs < 6 ) {
    weekTr += '</tr><tr>';
    for ( i=0; i<7; i++ ) weekTr += '<td width="34" height="25"> </td>';
    trs++;
  }
  document.write('<table width="238" border="1" borderColor="#CCCCCC" cellpadding="0" cellspacing="1" class="calTable">\n' + 
                 '<tr align="center" valign="middle">\n' + weekTitleTr + '</tr>' +
                 '<tr align="center" valign="middle">\n' + weekTr + '</tr>\n</table>\n');
}
 
function calTitle() {
  document.write(months[Number(pageMonth)] + ' ' + pageYear);
}
 
function change(funct, val) { // Changes month calendar w/OnChange of Month & Year drop downs
  if ( funct == 'month' ) {
    var monthVal = ( Number(val) < 10 ) ? '0' + Number(val) : val.toString();
    window.location = 'dayPicker.html?' + pageYear.toString() + '-' +
                      monthVal + '-' + pageDateDay.toString();
  } else if ( funct == 'year' ) {
    var monthInt = Number(pageMonth) - 1;
    monthInt = ( monthInt < 10 ) ? '0' + monthInt : monthInt.toString();
    window.location = 'dayPicker.html?' + val.toString() + '-' +
                      monthInt.toString() + '-' + pageDateDay.toString();
  }
}
-->
</script>
</head>
<body marginheight="0" marginwidth="0">
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0" class="tableBorder">
   <td valign="top">
      <form name="form">
        <table width="238" border="0" align="center" cellpadding="0" cellspacing="0">
          <tr> 
            <td align="center" nowrap class="calTitle">
            <script language="JavaScript" type="text/JavaScript">
            <!--
            calTitle();
            -->
            </script></td>
              <td align="right" nowrap class="calTitle"> <select name="month" onChange="change('month',document.form.month.options[document.form.month.selectedIndex].value);">
              <script language="JavaScript" type="text/javascript">
              <!--
              monthDropDown();
              -->
              </script>
              </select> <select name="year" onChange="change('year',document.form.year.options[document.form.year.selectedIndex].value);">
              <script language="JavaScript" type="text/javascript">
              <!--
              yearDropDown();
              -->
              </script>
              </select></td>
          </tr>
          <tr> 
            <td colspan="2" align="center">
            <script language="JavaScript" type="text/javascript">
            <!--
            monthCalendar();
            -->
            </script></td>
Edited by requinix
please use [code] tags when posting code
Link to comment
https://forums.phpfreaks.com/topic/300835-code-is-not-reflecting-feb-29th/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.