1
<script language="JavaScript" type="text/JavaScript">
  2
// 日期选择
  3
// By Ziyue(http://www.web-v.com/)
  4
var months = new Array("一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"); 
  5
var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 
  6
var days = new Array("日","一", "二", "三", "四", "五", "六"); 
  7
var today; 
  8
  9
document.writeln("<div id='Calendar' style='position:absolute; z-index:1; visibility: hidden; filter:\"progid:DXImageTransform.Microsoft.Shadow(direction=135,color=#999999,strength=3)\"'></div>");
 10
 11
function getDays(month, year)
 12

{ 
 13
//下面的这段代码是判断当前是否是闰年的 
 14
if (1 == month) 
 15
return ((0 == year % 4) && (0 != (year % 100))) || (0 == year % 400) ? 29 : 28; 
 16
else 
 17
return daysInMonth[month]; 
 18
} 
 19
 20
function getToday() 
 21

{ 
 22
//得到今天的年,月,日 
 23
this.now = new Date(); 
 24
this.year = this.now.getFullYear(); 
 25
this.month = this.now.getMonth(); 
 26
this.day = this.now.getDate(); 
 27
}
 28
 29
function getStringDay(str) 
 30

{ 
 31
//得到输入框的年,月,日
 32
 33
var str=str.split("-")
 34
 35
this.now = new Date(parseFloat(str[0]),parseFloat(str[1])-1,parseFloat(str[2])); 
 36
this.year = this.now.getFullYear(); 
 37
this.month = this.now.getMonth(); 
 38
this.day = this.now.getDate(); 
 39
}
 40
 41
function newCalendar() 
{ 
 42
var parseYear = parseInt(document.all.Year.options[document.all.Year.selectedIndex].value); 
 43
var newCal = new Date(parseYear, document.all.Month.selectedIndex, 1); 
 44
var day = -1; 
 45
var startDay = newCal.getDay(); 
 46
var daily = 0; 
 47
 48
if ((today.year == newCal.getFullYear()) &&(today.month == newCal.getMonth())) 
 49
day = today.day; 
 50
 51
var tableCal = document.all.calendar; 
 52
var intDaysInMonth =getDays(newCal.getMonth(), newCal.getFullYear());
 53
 54
for (var intWeek = 1; intWeek < tableCal.rows.length;intWeek++) 
 55
for (var intDay = 0;intDay < tableCal.rows[intWeek].cells.length;intDay++) 
 56

{ 
 57
var cell = tableCal.rows[intWeek].cells[intDay]; 
 58
if ((intDay == startDay) && (0 == daily)) 
 59
daily = 1; 
 60
 61
if(day==daily) //今天,调用今天的Class 
 62

{
 63
cell.style.background='#6699CC';
 64
cell.style.color='#FFFFFF';
 65
//cell.style.fontWeight='bold';
 66
}
 67
else if(intDay==6) //周六 
 68
cell.style.color='green'; 
 69
else if (intDay==0) //周日 
 70
cell.style.color='red';
 71
 72
if ((daily > 0) && (daily <= intDaysInMonth)) 
 73

{ 
 74
cell.innerText = daily; 
 75
daily++; 
 76
} 
 77
else 
 78
cell.innerText = ""; 
 79
} 
 80
} 
 81
 82
function GetDate(InputBox)
 83

{ 
 84
var sDate; 
 85
//这段代码处理鼠标点击的情况 
 86
if (event.srcElement.tagName == "TD") 
 87
if (event.srcElement.innerText != "") 
 88

{ 
 89
sDate = document.all.Year.value + "-" + document.all.Month.value + "-" + event.srcElement.innerText;
 90
eval("document.all."+InputBox).value=sDate;
 91
HiddenCalendar();
 92
} 
 93
} 
 94
 95
function HiddenCalendar()
 96

{
 97
//关闭选择窗口
 98
document.all.Calendar.style.visibility='hidden';
 99
}
100
101
function ShowCalendar(InputBox)
102

{
103
var x,y,intLoop,intWeeks,intDays;
104
var DivContent;
105
var year,month,day;
106
var o=eval("document.all."+InputBox);
107
var thisyear; //真正的今年年份
108
109
110
thisyear=new getToday();
111
thisyear=thisyear.year;
112
113
today = o.value;
114
if(isDate(today))
115
today = new getStringDay(today);
116
else
117
today = new getToday(); 
118
119
//显示的位置
120
121
x=o.offsetLeft;
122
y=o.offsetTop;
123
while(o=o.offsetParent)
124

{
125
x+=o.offsetLeft;
126
y+=o.offsetTop;
127
}
128
document.all.Calendar.style.left=x+2;
129
document.all.Calendar.style.top=y+20;
130
document.all.Calendar.style.visibility="visible";
131
132
//下面开始输出日历表格(border-color:#9DBAF7)
133
DivContent="<table border='0' cellspacing='0' style='border:1px solid #0066FF; background-color:#EDF2FC'>";
134
DivContent+="<tr>";
135
DivContent+="<td style='border-bottom:1px solid #0066FF; background-color:#C7D8FA'>";
136
137
//年
138
139
DivContent+="<select name='Year' id='Year' onChange='newCalendar()' style='font-family:Verdana; font-size:12px'>";
140
for (intLoop = thisyear - 100; intLoop < (thisyear + 2); intLoop++) 
141
DivContent+="<option value= " + intLoop + " " + (today.year == intLoop ? "Selected" : "") + ">" + intLoop + "</option>"; 
142
DivContent+="</select>";
143
144
//月
145
146
DivContent+="<select name='Month' id='Month' onChange='newCalendar()' style='font-family:Verdana; font-size:12px'>";
147
for (intLoop = 0; intLoop < months.length; intLoop++) 
148
DivContent+="<option value= " + (intLoop + 1) + " " + (today.month == intLoop ? "Selected" : "") + ">" + months[intLoop] + "</option>"; 
149
DivContent+="</select>";
150
151
DivContent+="</td>";
152
153
DivContent+="<td style='border-bottom:1px solid #0066FF; background-color:#C7D8FA; font-weight:bold; font-family:Wingdings 2,Wingdings,Webdings; font-size:16px; padding-top:2px; color:#4477FF; cursor:hand' align='center' title='关闭' onClick='javascript:HiddenCalendar()'>S</td>";
154
DivContent+="</tr>";
155
156
DivContent+="<tr><td align='center' colspan='2'>";
157
DivContent+="<table id='calendar' border='0' width='100%'>";
158
159
//星期
160
DivContent+="<tr>";
161
for (intLoop = 0; intLoop < days.length; intLoop++) 
162
DivContent+="<td align='center' style='font-size:12px'>" + days[intLoop] + "</td>"; 
163
DivContent+="</tr>";
164
165
//天
166
167
for (intWeeks = 0; intWeeks < 6; intWeeks++)
168

{ 
169
DivContent+="<tr>"; 
170
for (intDays = 0; intDays < days.length; intDays++) 
171
DivContent+="<td onClick='GetDate(\"" + InputBox + "\")' style='cursor:hand; border-right:1px solid #BBBBBB; border-bottom:1px solid #BBBBBB; color:#215DC6; font-family:Verdana; font-size:12px' align='center'></td>"; 
172
DivContent+="</tr>"; 
173
} 
174
DivContent+="</table></td></tr></table>";
175
176
document.all.Calendar.innerHTML=DivContent;
177
newCalendar();
178
}
179
180
function isDate(dateStr)
181

{ 
182
var datePat = /^(\d
{4})(\-)(\d
{1,2})(\-)(\d
{1,2})$/;
183
var matchArray = dateStr.match(datePat);
184
if (matchArray == null) return false; 
185
var month = matchArray[3];
186
var day = matchArray[5]; 
187
var year = matchArray[1]; 
188
if (month < 1 || month > 12) return false; 
189
if (day < 1 || day > 31) return false; 
190
if ((month==4 || month==6 || month==9 || month==11) && day==31) return false; 
191
if (month == 2)
192

{
193
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); 
194
if (day > 29 || (day==29 && !isleap)) return false; 
195
} 
196
return true;
197
}
198
</script>
199
200
 
201
202
 
1
使用方法
2
3
为
4
5
 <input type="text" id="birthDay" name="birthDay" style="width: 150px;" onclick="javascript:ShowCalendar(this.id)">
6
7
 
转自:http://blog.163.com/afishman@126/blog/static/47122647200853101434314/