随笔-46  评论-54  文章-0  trackbacks-0
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <title> javascript string to date by pattern </title>
5 </head>
6 <body>
7 <script language="javascript">
8
9 /** year : /yyyy/ */
10 var y4 = "([0-9]{4})";
11 /** year : /yy/ */
12 var y2 = "([0-9]{2})";
13 /** index year */
14 var yi = -1;
15
16 /** month : /MM/ */
17 var M2 = "(0[1-9]|1[0-2])";
18 /** month : /M/ */
19 var M1 = "([1-9]|1[0-2])";
20 /** index month */
21 var Mi = -1;
22
23 /** day : /dd/ */
24 var d2 = "(0[1-9]|[1-2][0-9]|30|31)";
25 /** day : /d/ */
26 var d1 = "([1-9]|[1-2][0-9]|30|31)";
27 /** index day */
28 var di = -1;
29
30 /** hour : /HH/ */
31 var H2 = "([0-1][0-9]|20|21|22|23)";
32 /** hour : /H/ */
33 var H1 = "([0-9]|1[0-9]|20|21|22|23)";
34 /** index hour */
35 var Hi = -1;
36
37 /** minute : /mm/ */
38 var m2 = "([0-5][0-9])";
39 /** minute : /m/ */
40 var m1 = "([0-9]|[1-5][0-9])";
41 /** index minute */
42 var mi = -1;
43
44 /** second : /ss/ */
45 var s2 = "([0-5][0-9])";
46 /** second : /s/ */
47 var s1 = "([0-9]|[1-5][0-9])";
48 /** index month */
49 var si = -1;
50
51 var regexp;
52
53 function getDate(dateString, formatString){
54 if(validateDate(dateString, formatString)) {
55 var now = new Date();
56 var vals = regexp.exec(dateString);
57 var index = validateIndex(formatString);
58 var year = index[0]>=0?vals[index[0] + 1]:now.getFullYear();
59 var month = index[1]>=0?(vals[index[1] + 1]-1):now.getMonth();
60 var day = index[2]>=0?vals[index[2] + 1]:now.getDate();
61 var hour = index[3]>=0?vals[index[3] + 1]:"";
62 var minute = index[4]>=0?vals[index[4] + 1]:"";
63 var second = index[5]>=0?vals[index[5] + 1]:"";
64
65 var validate;
66
67 if (hour == "")
68 validate = new Date(year, month, day);
69 else
70 validate = new Date(year, month, day, hour, minute, second);
71
72 if(validate.getDate()==day) return validate;
73
74 }
75 alert("wrong date");
76 }
77
78
79 function validateDate(dateString, formatString){
80 var dateString = trim(dateString);
81 if(dateString=="") return;
82 var reg = formatString;
83 reg = reg.replace(/yyyy/, y4);
84 reg = reg.replace(/yy/, y2);
85 reg = reg.replace(/MM/, M2);
86 reg = reg.replace(/M/, M1);
87 reg = reg.replace(/dd/, d2);
88 reg = reg.replace(/d/, d1);
89 reg = reg.replace(/HH/, H2);
90 reg = reg.replace(/H/, H1);
91 reg = reg.replace(/mm/, m2);
92 reg = reg.replace(/m/, m1);
93 reg = reg.replace(/ss/, s2);
94 reg = reg.replace(/s/, s1);
95 reg = new RegExp("^"+reg+"$");
96 regexp = reg;
97 return reg.test(dateString);
98 }
99
100 function validateIndex(formatString){
101
102 var ia = new Array();
103 var i = 0;
104 yi = formatString.search(/yyyy/);
105 if ( yi < 0 ) yi = formatString.search(/yy/);
106 if (yi >= 0) {
107 ia[i] = yi;
108 i++;
109 }
110
111 Mi = formatString.search(/MM/);
112 if ( Mi < 0 ) Mi = formatString.search(/M/);
113 if (Mi >= 0) {
114 ia[i] = Mi;
115 i++;
116 }
117
118 di = formatString.search(/dd/);
119 if ( di < 0 ) di = formatString.search(/d/);
120 if (di >= 0) {
121 ia[i] = di;
122 i++;
123 }
124
125 Hi = formatString.search(/HH/);
126 if ( Hi < 0 ) Hi = formatString.search(/H/);
127 if (Hi >= 0) {
128 ia[i] = Hi;
129 i++;
130 }
131
132 mi = formatString.search(/mm/);
133 if ( mi < 0 ) mi = formatString.search(/m/);
134 if (mi >= 0) {
135 ia[i] = mi;
136 i++;
137 }
138
139 si = formatString.search(/ss/);
140 if ( si < 0 ) si = formatString.search(/s/);
141 if (si >= 0) {
142 ia[i] = si;
143 i++;
144 }
145
146 var ia2 = new Array(yi, Mi, di, Hi, mi, si);
147
148 for(i=0; i<ia.length-1; i++)
149 for(j=0;j<ia.length-1-i;j++)
150 if(ia[j]>ia[j+1]) {
151 temp=ia[j];
152 ia[j]=ia[j+1];
153 ia[j+1]=temp;
154 }
155
156 for (i=0; i<ia.length ; i++)
157 for (j=0; j<ia2.length; j++)
158 if(ia[i]==ia2[j]) {
159 ia2[j] = i;
160 }
161
162 return ia2;
163 }
164
165
166 function trim(str){
167 return str.replace(/(^\s*)|(\s*$)/g, "");
168 }
169
170 //-->
171 </script>
172
173 <FORM METHOD=POST ACTION="">
174 <INPUT TYPE="text" NAME="dater" value="02-29-2007">
175 <INPUT TYPE="text" NAME="formater" value="MM-dd-yyyy">
176 <INPUT TYPE="text" NAME="timer" value="" size="35">
177 <INPUT TYPE="submit" onclick="this.form.timer.value=getDate(this.form.dater.value, this.form.formater.value);return false;">
178 </FORM>
179
180 <FORM METHOD=POST ACTION="">
181 <INPUT TYPE="text" NAME="dater" value="2004-2-29">
182 <INPUT TYPE="text" NAME="formater" value="yyyy-M-d">
183 <INPUT TYPE="text" NAME="timer" value="" size="35">
184 <INPUT TYPE="submit" onclick="this.form.timer.value=getDate(this.form.dater.value, this.form.formater.value);return false;">
185 </FORM>
186
187 <FORM METHOD=POST ACTION="">
188 <INPUT TYPE="text" NAME="dater" value="12:56:34">
189 <INPUT TYPE="text" NAME="formater" value="HH:mm:ss">
190 <INPUT TYPE="text" NAME="timer" value="" size="35">
191 <INPUT TYPE="submit" onclick="this.form.timer.value=getDate(this.form.dater.value, this.form.formater.value);return false;">
192 </FORM>
193
194 <FORM METHOD=POST ACTION="">
195 <INPUT TYPE="text" NAME="dater" value="2004-02-26 12:56:34">
196 <INPUT TYPE="text" NAME="formater" value="yyyy-MM-dd HH:mm:ss">
197 <INPUT TYPE="text" NAME="timer" value="" size="35">
198 <INPUT TYPE="submit" onclick="this.form.timer.value=getDate(this.form.dater.value, this.form.formater.value);return false;">
199 </FORM>
200
201 <FORM METHOD=POST ACTION="">
202 <INPUT TYPE="text" NAME="dater" value="20040226125634">
203 <INPUT TYPE="text" NAME="formater" value="yyyyMMddHHmmss">
204 <INPUT TYPE="text" NAME="timer" value="" size="35">
205 <INPUT TYPE="submit" onclick="this.form.timer.value=getDate(this.form.dater.value, this.form.formater.value);return false;">
206 </FORM>
207
208 </body>
209 </html>
validateDateClean.html
posted on 2008-12-30 00:08 rox 阅读(20149) 评论(2)  编辑  收藏

评论:
# re: js根据字符串样式转换成日期类型(javascript string to date by pattern) 2009-01-23 01:32 | rox
# re: js根据字符串样式转换成日期类型(javascript string to Date by pattern) 2014-06-24 09:19 | rox
My97DatePicker日历控件实现年、季、月、周、日选择;季度选择是用月选择模拟的,无法隐藏月度选择和前进后退箭头
http://runjs.cn/detail/wu0o0p68  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: