1<script language="JavaScript" type="text/JavaScript">
2// 日期选择
3// By Ziyue(http://www.web-v.com/)
4var months = new Array("一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月");
5var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
6var days = new Array("日","一", "二", "三", "四", "五", "六");
7var today;
8
9document.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
11function getDays(month, year)
12{
13//下面的这段代码是判断当前是否是闰年的
14if (1 == month)
15return ((0 == year % 4) && (0 != (year % 100))) || (0 == year % 400) ? 29 : 28;
16else
17return daysInMonth[month];
18}
19
20function getToday()
21{
22//得到今天的年,月,日
23this.now = new Date();
24this.year = this.now.getFullYear();
25this.month = this.now.getMonth();
26this.day = this.now.getDate();
27}
28
29function getStringDay(str)
30{
31//得到输入框的年,月,日
32
33var str=str.split("-")
34
35this.now = new Date(parseFloat(str[0]),parseFloat(str[1])-1,parseFloat(str[2]));
36this.year = this.now.getFullYear();
37this.month = this.now.getMonth();
38this.day = this.now.getDate();
39}
40
41function newCalendar() {
42var parseYear = parseInt(document.all.Year.options[document.all.Year.selectedIndex].value);
43var newCal = new Date(parseYear, document.all.Month.selectedIndex, 1);
44var day = -1;
45var startDay = newCal.getDay();
46var daily = 0;
47
48if ((today.year == newCal.getFullYear()) &&(today.month == newCal.getMonth()))
49day = today.day;
50
51var tableCal = document.all.calendar;
52var intDaysInMonth =getDays(newCal.getMonth(), newCal.getFullYear());
53
54for (var intWeek = 1; intWeek < tableCal.rows.length;intWeek++)
55for (var intDay = 0;intDay < tableCal.rows[intWeek].cells.length;intDay++)
56{
57var cell = tableCal.rows[intWeek].cells[intDay];
58if ((intDay == startDay) && (0 == daily))
59daily = 1;
60
61if(day==daily) //今天,调用今天的Class
62{
63cell.style.background='#6699CC';
64cell.style.color='#FFFFFF';
65//cell.style.fontWeight='bold';
66}
67else if(intDay==6) //周六
68cell.style.color='green';
69else if (intDay==0) //周日
70cell.style.color='red';
71
72if ((daily > 0) && (daily <= intDaysInMonth))
73{
74cell.innerText = daily;
75daily++;
76}
77else
78cell.innerText = "";
79}
80}
81
82function GetDate(InputBox)
83{
84var sDate;
85//这段代码处理鼠标点击的情况
86if (event.srcElement.tagName == "TD")
87if (event.srcElement.innerText != "")
88{
89sDate = document.all.Year.value + "-" + document.all.Month.value + "-" + event.srcElement.innerText;
90eval("document.all."+InputBox).value=sDate;
91HiddenCalendar();
92}
93}
94
95function HiddenCalendar()
96{
97//关闭选择窗口
98document.all.Calendar.style.visibility='hidden';
99}
100
101function ShowCalendar(InputBox)
102{
103var x,y,intLoop,intWeeks,intDays;
104var DivContent;
105var year,month,day;
106var o=eval("document.all."+InputBox);
107var thisyear; //真正的今年年份
108
109
110thisyear=new getToday();
111thisyear=thisyear.year;
112
113today = o.value;
114if(isDate(today))
115today = new getStringDay(today);
116else
117today = new getToday();
118
119//显示的位置
120
121x=o.offsetLeft;
122y=o.offsetTop;
123while(o=o.offsetParent)
124{
125x+=o.offsetLeft;
126y+=o.offsetTop;
127}
128document.all.Calendar.style.left=x+2;
129document.all.Calendar.style.top=y+20;
130document.all.Calendar.style.visibility="visible";
131
132//下面开始输出日历表格(border-color:#9DBAF7)
133DivContent="<table border='0' cellspacing='0' style='border:1px solid #0066FF; background-color:#EDF2FC'>";
134DivContent+="<tr>";
135DivContent+="<td style='border-bottom:1px solid #0066FF; background-color:#C7D8FA'>";
136
137//年
138
139DivContent+="<select name='Year' id='Year' onChange='newCalendar()' style='font-family:Verdana; font-size:12px'>";
140for (intLoop = thisyear - 100; intLoop < (thisyear + 2); intLoop++)
141DivContent+="<option value= " + intLoop + " " + (today.year == intLoop ? "Selected" : "") + ">" + intLoop + "</option>";
142DivContent+="</select>";
143
144//月
145
146DivContent+="<select name='Month' id='Month' onChange='newCalendar()' style='font-family:Verdana; font-size:12px'>";
147for (intLoop = 0; intLoop < months.length; intLoop++)
148DivContent+="<option value= " + (intLoop + 1) + " " + (today.month == intLoop ? "Selected" : "") + ">" + months[intLoop] + "</option>";
149DivContent+="</select>";
150
151DivContent+="</td>";
152
153DivContent+="<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>";
154DivContent+="</tr>";
155
156DivContent+="<tr><td align='center' colspan='2'>";
157DivContent+="<table id='calendar' border='0' width='100%'>";
158
159//星期
160DivContent+="<tr>";
161for (intLoop = 0; intLoop < days.length; intLoop++)
162DivContent+="<td align='center' style='font-size:12px'>" + days[intLoop] + "</td>";
163DivContent+="</tr>";
164
165//天
166
167for (intWeeks = 0; intWeeks < 6; intWeeks++)
168{
169DivContent+="<tr>";
170for (intDays = 0; intDays < days.length; intDays++)
171DivContent+="<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>";
172DivContent+="</tr>";
173}
174DivContent+="</table></td></tr></table>";
175
176document.all.Calendar.innerHTML=DivContent;
177newCalendar();
178}
179
180function isDate(dateStr)
181{
182var datePat = /^(\d{4})(\-)(\d{1,2})(\-)(\d{1,2})$/;
183var matchArray = dateStr.match(datePat);
184if (matchArray == null) return false;
185var month = matchArray[3];
186var day = matchArray[5];
187var year = matchArray[1];
188if (month < 1 || month > 12) return false;
189if (day < 1 || day > 31) return false;
190if ((month==4 || month==6 || month==9 || month==11) && day==31) return false;
191if (month == 2)
192{
193var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
194if (day > 29 || (day==29 && !isleap)) return false;
195}
196return 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/