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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

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/

2

3

4

5

6

7
