1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
4<meta name="Author" content="Jiang Hongbin">
5<title>日期选择 - BY ziyue</title>
6<script language="JavaScript" type="text/JavaScript">
7// 日期选择
8// By Ziyue(http://www.web-v.com/)
9var months = new Array("一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月");
10var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
11var days = new Array("日","一", "二", "三", "四", "五", "六");
12var today;
13
14document.writeln("<div id='Calendar' style='position:absolute; z-index:1; visibility: hidden; filter:\"progid:DXImageTransform.Microsoft.Shadow(direction=135,color=#999999,strength=3)\"'></div>");
15
16function getDays(month, year)
17{
18//下面的这段代码是判断当前是否是闰年的
19if (1 == month)
20return ((0 == year % 4) && (0 != (year % 100))) || (0 == year % 400) ? 29 : 28;
21else
22return daysInMonth[month];
23}
24
25function getToday()
26{
27//得到今天的年,月,日
28this.now = new Date();
29thisthis.year = this.now.getFullYear();
30thisthis.month = this.now.getMonth();
31thisthis.day = this.now.getDate();
32}
33
34function getStringDay(str)
35{
36//得到输入框的年,月,日
37var strstr=str.split("-")
38
39this.now = new Date(parseFloat(str[0]),parseFloat(str[1])-1,parseFloat(str[2]));
40thisthis.year = this.now.getFullYear();
41thisthis.month = this.now.getMonth();
42thisthis.day = this.now.getDate();
43}
44
45function newCalendar() {
46var parseYear = parseInt(document.all.Year.options[document.all.Year.selectedIndex].value);
47var newnewCal = new Date(parseYear, document.all.Month.selectedIndex, 1);
48var day = -1;
49var startDay = newCal.getDay();
50var daily = 0;
51
52if ((today.year == newCal.getFullYear()) &&(today.month == newCal.getMonth()))
53day = today.day;
54
55var tableCal = document.all.calendar;
56var intDaysInMonth =getDays(newCal.getMonth(), newCal.getFullYear());
57
58for (var intWeek = 1; intWeek < tableCal.rows.length;intWeek++)
59for (var intDay = 0;intDay < tableCal.rows[intWeek].cells.length;intDay++)
60{
61var cell = tableCal.rows[intWeek].cells[intDay];
62if ((intDay == startDay) && (0 == daily))
63daily = 1;
64
65if(day==daily) //今天,调用今天的Class
66{
67cell.style.background='#6699CC';
68cell.style.color='#FFFFFF';
69//cell.style.fontWeight='bold';
70}
71else if(intDay==6) //周六
72cell.style.color='green';
73else if (intDay==0) //周日
74cell.style.color='red';
75
76if ((daily > 0) && (daily <= intDaysInMonth))
77{
78cell.innerText = daily;
79daily++;
80}
81else
82cell.innerText = "";
83}
84}
85
86function GetDate(InputBox)
87{
88var sDate;
89//这段代码处理鼠标点击的情况
90if (event.srcElement.tagName == "TD")
91if (event.srcElement.innerText != "")
92{
93sDate = document.all.Year.value + "-" + document.all.Month.value + "-" + event.srcElement.innerText;
94eval("document.all."+InputBox).value=sDate;
95HiddenCalendar();
96}
97}
98
99function HiddenCalendar()
100{
101//关闭选择窗口
102document.all.Calendar.style.visibility='hidden';
103}
104
105function ShowCalendar(InputBox)
106{
107var x,y,intLoop,intWeeks,intDays;
108var DivContent;
109var year,month,day;
110var o=eval("document.all."+InputBox);
111var thisyear; //真正的今年年份
112
113thisyear=new getToday();
114thisyearthisyear=thisyear.year;
115
116today = o.value;
117if(isDate(today))
118today = new getStringDay(today);
119else
120today = new getToday();
121
122//显示的位置
123x=o.offsetLeft;
124y=o.offsetTop;
125while(oo=o.offsetParent)
126{
127x+=o.offsetLeft;
128y+=o.offsetTop;
129}
130document.all.Calendar.style.left=x+2;
131document.all.Calendar.style.top=y+20;
132document.all.Calendar.style.visibility="visible";
133
134//下面开始输出日历表格(border-color:#9DBAF7)
135DivContent="<table border='0' cellspacing='0' style='border:1px solid #0066FF; background-color:#EDF2FC'>";
136DivContent+="<tr>";
137DivContent+="<td style='border-bottom:1px solid #0066FF; background-color:#C7D8FA'>";
138
139//年
140DivContent+="<select name='Year' id='Year' onChange='newCalendar()' style='font-family:Verdana; font-size:12px'>";
141for (intLoop = thisyear - 35; intLoop < (thisyear + 2); intLoop++)
142DivContent+="<option value= " + intLoop + " " + (today.year == intLoop ? "Selected" : "") + ">" + intLoop + "</option>";
143DivContent+="</select>";
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//天
166for (intWeeks = 0; intWeeks < 6; intWeeks++)
167{
168DivContent+="<tr>";
169for (intDays = 0; intDays < days.length; intDays++)
170DivContent+="<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>";
171DivContent+="</tr>";
172}
173DivContent+="</table></td></tr></table>";
174
175document.all.Calendar.innerHTML=DivContent;
176newCalendar();
177}
178
179function isDate(dateStr)
180{
181var datePat = /^(\d{4})(\-)(\d{1,2})(\-)(\d{1,2})$/;
182var matchArray = dateStr.match(datePat);
183if (matchArray == null) return false;
184var month = matchArray[3];
185var day = matchArray[5];
186var year = matchArray[1];
187if (month < 1 || month > 12) return false;
188if (day < 1 || day > 31) return false;
189if ((month==4 || month==6 || month==9 || month==11) && day==31) return false;
190if (month == 2)
191{
192var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
193if (day > 29 || (day==29 && !isleap)) return false;
194}
195return true;
196}
197</script>
198<style type="text/css">
199<!--
200td,input {
201font-family: Verdana, Arial, Helvetica, sans-serif;
202font-size: 12px;
203}
204-->
205</style>
206</head>
207
208<body>
209<form name="form1" method="post" action="">
210<table width="500" border="0" align="center" cellspacing="1" bgcolor="#CCCCCC">
211<tr bgcolor="#FFFFFF">
212<td align="right">您的生日:</td>
213<td><input name="birthday" type="text" id="birthday" title="点击选择" onClick="javascript:ShowCalendar(this.id)" size="20">
214<input type="button" name="Submit" value="选 择" onClick="javascript:ShowCalendar('birthday')"></td>
215</tr>
216</table>
217</form>
218</body>
219</html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
4<meta name="Author" content="Jiang Hongbin">
5<title>日期选择 - BY ziyue</title>
6<script language="JavaScript" type="text/JavaScript">
7// 日期选择
8// By Ziyue(http://www.web-v.com/)
9var months = new Array("一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月");
10var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
11var days = new Array("日","一", "二", "三", "四", "五", "六");
12var today;
13
14document.writeln("<div id='Calendar' style='position:absolute; z-index:1; visibility: hidden; filter:\"progid:DXImageTransform.Microsoft.Shadow(direction=135,color=#999999,strength=3)\"'></div>");
15
16function getDays(month, year)
17{
18//下面的这段代码是判断当前是否是闰年的
19if (1 == month)
20return ((0 == year % 4) && (0 != (year % 100))) || (0 == year % 400) ? 29 : 28;
21else
22return daysInMonth[month];
23}
24
25function getToday()
26{
27//得到今天的年,月,日
28this.now = new Date();
29thisthis.year = this.now.getFullYear();
30thisthis.month = this.now.getMonth();
31thisthis.day = this.now.getDate();
32}
33
34function getStringDay(str)
35{
36//得到输入框的年,月,日
37var strstr=str.split("-")
38
39this.now = new Date(parseFloat(str[0]),parseFloat(str[1])-1,parseFloat(str[2]));
40thisthis.year = this.now.getFullYear();
41thisthis.month = this.now.getMonth();
42thisthis.day = this.now.getDate();
43}
44
45function newCalendar() {
46var parseYear = parseInt(document.all.Year.options[document.all.Year.selectedIndex].value);
47var newnewCal = new Date(parseYear, document.all.Month.selectedIndex, 1);
48var day = -1;
49var startDay = newCal.getDay();
50var daily = 0;
51
52if ((today.year == newCal.getFullYear()) &&(today.month == newCal.getMonth()))
53day = today.day;
54
55var tableCal = document.all.calendar;
56var intDaysInMonth =getDays(newCal.getMonth(), newCal.getFullYear());
57
58for (var intWeek = 1; intWeek < tableCal.rows.length;intWeek++)
59for (var intDay = 0;intDay < tableCal.rows[intWeek].cells.length;intDay++)
60{
61var cell = tableCal.rows[intWeek].cells[intDay];
62if ((intDay == startDay) && (0 == daily))
63daily = 1;
64
65if(day==daily) //今天,调用今天的Class
66{
67cell.style.background='#6699CC';
68cell.style.color='#FFFFFF';
69//cell.style.fontWeight='bold';
70}
71else if(intDay==6) //周六
72cell.style.color='green';
73else if (intDay==0) //周日
74cell.style.color='red';
75
76if ((daily > 0) && (daily <= intDaysInMonth))
77{
78cell.innerText = daily;
79daily++;
80}
81else
82cell.innerText = "";
83}
84}
85
86function GetDate(InputBox)
87{
88var sDate;
89//这段代码处理鼠标点击的情况
90if (event.srcElement.tagName == "TD")
91if (event.srcElement.innerText != "")
92{
93sDate = document.all.Year.value + "-" + document.all.Month.value + "-" + event.srcElement.innerText;
94eval("document.all."+InputBox).value=sDate;
95HiddenCalendar();
96}
97}
98
99function HiddenCalendar()
100{
101//关闭选择窗口
102document.all.Calendar.style.visibility='hidden';
103}
104
105function ShowCalendar(InputBox)
106{
107var x,y,intLoop,intWeeks,intDays;
108var DivContent;
109var year,month,day;
110var o=eval("document.all."+InputBox);
111var thisyear; //真正的今年年份
112
113thisyear=new getToday();
114thisyearthisyear=thisyear.year;
115
116today = o.value;
117if(isDate(today))
118today = new getStringDay(today);
119else
120today = new getToday();
121
122//显示的位置
123x=o.offsetLeft;
124y=o.offsetTop;
125while(oo=o.offsetParent)
126{
127x+=o.offsetLeft;
128y+=o.offsetTop;
129}
130document.all.Calendar.style.left=x+2;
131document.all.Calendar.style.top=y+20;
132document.all.Calendar.style.visibility="visible";
133
134//下面开始输出日历表格(border-color:#9DBAF7)
135DivContent="<table border='0' cellspacing='0' style='border:1px solid #0066FF; background-color:#EDF2FC'>";
136DivContent+="<tr>";
137DivContent+="<td style='border-bottom:1px solid #0066FF; background-color:#C7D8FA'>";
138
139//年
140DivContent+="<select name='Year' id='Year' onChange='newCalendar()' style='font-family:Verdana; font-size:12px'>";
141for (intLoop = thisyear - 35; intLoop < (thisyear + 2); intLoop++)
142DivContent+="<option value= " + intLoop + " " + (today.year == intLoop ? "Selected" : "") + ">" + intLoop + "</option>";
143DivContent+="</select>";
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//天
166for (intWeeks = 0; intWeeks < 6; intWeeks++)
167{
168DivContent+="<tr>";
169for (intDays = 0; intDays < days.length; intDays++)
170DivContent+="<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>";
171DivContent+="</tr>";
172}
173DivContent+="</table></td></tr></table>";
174
175document.all.Calendar.innerHTML=DivContent;
176newCalendar();
177}
178
179function isDate(dateStr)
180{
181var datePat = /^(\d{4})(\-)(\d{1,2})(\-)(\d{1,2})$/;
182var matchArray = dateStr.match(datePat);
183if (matchArray == null) return false;
184var month = matchArray[3];
185var day = matchArray[5];
186var year = matchArray[1];
187if (month < 1 || month > 12) return false;
188if (day < 1 || day > 31) return false;
189if ((month==4 || month==6 || month==9 || month==11) && day==31) return false;
190if (month == 2)
191{
192var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
193if (day > 29 || (day==29 && !isleap)) return false;
194}
195return true;
196}
197</script>
198<style type="text/css">
199<!--
200td,input {
201font-family: Verdana, Arial, Helvetica, sans-serif;
202font-size: 12px;
203}
204-->
205</style>
206</head>
207
208<body>
209<form name="form1" method="post" action="">
210<table width="500" border="0" align="center" cellspacing="1" bgcolor="#CCCCCC">
211<tr bgcolor="#FFFFFF">
212<td align="right">您的生日:</td>
213<td><input name="birthday" type="text" id="birthday" title="点击选择" onClick="javascript:ShowCalendar(this.id)" size="20">
214<input type="button" name="Submit" value="选 择" onClick="javascript:ShowCalendar('birthday')"></td>
215</tr>
216</table>
217</form>
218</body>
219</html>