build a dynamic select list

[Total: 0    Average: 0/5]

This sets the select list items 0 1 and 2 to be the days dates for yesterday today and tomorrow.

Head Code:

<script>
Date.prototype.Months = ["January", "February", "March",
                         "April", "May", "June", "July",
                         "August", "September", "October",
                         "November", "December"];
Date.prototype.Days = ["Sunday", "Monday", "Tuesday",
                       "Wednesday", "Thursday",
                       "Friday", "Saturday"];
Date.prototype.format = dateFormat;
function dateFormat(format) {
   var dateString = format;    //yyyy  is a 4-digit year - i.e., 2002 
   dateString = dateString.replace( new RegExp("yyyy", "gi"), this.getYear() );
   //yy    is a 2-digit year - i.e., 02
   dateString = dateString.replace( new RegExp("yy", "gi"), new String( this.getYear() ).substring(2,4) );
   //month is the full month - i.e., September
   dateString = dateString.replace( new RegExp("month", "gi"), this.Months[this.getMonth()] );
   //mon   is the first three letters of the month - i.e., Sep
   dateString = dateString.replace( new RegExp("mon", "gi"), new String( this.Months[this.getMonth()] ).substring(0,3) );
   //mmm   is the number of the month - i.e., 9
   dateString = dateString.replace( new RegExp("mmm", "gi"), (this.getMonth() + 1) );  
   //hh    is hours - i.e., 3
   dateString = dateString.replace( new RegExp("hh", "gi"), this.getHours() );
   //mm    is minutes (always 2-digit) - i.e., 05
   var mm = new String( this.getMinutes() );
   if (mm.length == 1) mm = "0" + mm; //pad if single digit
   dateString = dateString.replace( new RegExp("mm", "gi"), mm );
   //ss    is seconds (always 2-digit) - i.e., 08
   var ss = new String( this.getSeconds() );
   if (ss.length == 1) ss = "0" + mm; //pad if single digit
   dateString = dateString.replace( new RegExp("ss", "gi"), ss );
   //ddd   is the first three letters of the day - i.e., Wed
   dateString = dateString.replace( new RegExp("ddd", "gi"), new String( this.Days[this.getDay()] ).substring(0,3) );
   //dd    is the numerical day of the month - i.e, 25
   dateString = dateString.replace( new RegExp("dd", "gi"), this.getDate() );
   //day   is the full day of the week - i.e., Wednesday
   dateString = dateString.replace( new RegExp("day", "gi"), this.Days[this.getDay()] );    //timezone is the the timezone in hours from GMT - i.e., GMT+5
   tz = this.getTimezoneOffset();
   timezone = "";
   if (tz < 0)
      timezone = "GMT-" +  tz / 60;
   else if (tz == 0)
      timezone = "GMT";
   else
      timezone = "GMT+" + tz / 60;
   dateString = dateString.replace( new RegExp("timezone", "gi"), timezone );
  
   //time24   is the time based on a 24 hour clock - i.e., 18:24  
   var minutes = new String( this.getMinutes() );
   if (minutes.length == 1) minutes = "0" + minutes; //pad if single digit
   var time24 = new String( this.getHours() + ":" + minutes );
   dateString = dateString.replace( new RegExp("time24", "gi"), time24 );
  
   //time     is the time based on am/pm - i.e., 6:24PM
   var time;
   var ampm;
   var hour = this.getHours();
   if ( hour < 12) {
      if (hour == 0) hour = 12;
         ampm = "AM"
   } else {
      if (hour !=12)
         hour = hour - 12;
      ampm = "PM";  
   }
   time = new String(hour + ":" + minutes + ampm);    
   dateString = dateString.replace( new RegExp("time", "gi"), time );    return dateString;  
} 


function choosedate()
{
var dayDate  = new Date();
var prevDate = new Date(dayDate.getTime() - 1 * 24 * 60 * 60 * 1000); 
var nextDate = new Date(dayDate.getTime() + 1 * 24 * 60 * 60 * 1000); 

document.timetable.date1.options[0].value = prevDate;
document.timetable.date1.options[0].text  = prevDate.format('mmm/dd/yyyy');
document.timetable.date1.options[1].value = dayDate;
document.timetable.date1.options[1].text  = dayDate.format('mmm/dd/yyyy');
document.timetable.date1.options[2].value = nextDate;
document.timetable.date1.options[2].text  = nextDate.format('mmm/dd/yyyy');
}

</SCRIPT>

Body Code:

 <FORM name="timetable">      
      <SELECT name=date1> 
<OPTION value="" >
<OPTION value="" >
<OPTION value="" >
</SELECT> 
<script>
choosedate();
</script>

Times Viewed: 5

Leave a Reply

Your email address will not be published. Required fields are marked *