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



浙公网安备 33010602011771号