javascript 操作日期 测试(注意浏览器兼容问题)
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 操作日期 测试</title>
5<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6<META HTTP-EQUIV="REFRESH" CONTENT="11; URL=http://www.dusystem.com/index.htm"> 2006-03-12 designer Geovin Du-->
7<meta name="description" content="涂聚文,缔友计算机信息技术有限公司,经营信息流,物流,人力资源流,资本流的系统解决方案的开发与设计和服务,geoVI studio.Geovin Du. Systems Solution to Fund flow,and Information flow,and Material flow,and Control flow,and Human Resource.Applied software development,design and service。.">
8<meta name="description" content="Study,Communicate。 ">
9<meta name="Robots" content="all index follow ">
10<meta name="Author" content="涂聚文" />
11<link rel="shortcut icon" href="http://www.dusystem.com/favicon.ico" type="image/x-icon" />
12<link rel="icon" href="http://www.dusystem.com/favicon.ico" type="image/ico" />
13<link rel="Bookmark" href="http://www.dusystem.com/favicon.ico">
14 <script language="JavaScript">
15//此代碼,在IE6上不兼容,FireFox可以。
16 function DateAdd(strInterval, NumDay, dtDate) {
17
18 var dtTmp = new Date(dtDate);
19
20 if (isNaN(dtTmp)) dtTmp = new Date();
21
22 switch (strInterval) {
23
24 case "s":
25
26 dtTmp = new Date(Date.parse(dtTmp) + (1000 * parseInt(NumDay)));
27
28 break;
29
30 case "n":
31
32 dtTmp = new Date(Date.parse(dtTmp) + (60000 * parseInt(NumDay)));
33
34 break;
35
36 case "h":
37
38 dtTmp = new Date(Date.parse(dtTmp) + (3600000 * parseInt(NumDay)));
39
40 break;
41
42 case "d":
43
44 dtTmp = new Date(Date.parse(dtTmp) + (86400000 * parseInt(NumDay)));
45
46 break;
47
48 case "w":
49
50 dtTmp = new Date(Date.parse(dtTmp) + ((86400000 * 7) * parseInt(NumDay)));
51
52 break;
53
54 case "m":
55
56 dtTmp = new Date(dtTmp.getFullYear(), (dtTmp.getMonth())+parseInt(NumDay), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
57
58 break;
59
60 case "y":
61
62 //alert(dtTmp.getFullYear());
63
64 dtTmp = new Date(dtTmp.getFullYear()+parseInt(NumDay), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
65
66 //alert(dtTmp);
67
68 break;
69
70 }
71
72 var mStr=new String(dtTmp.getMonth()+1);
73
74 var dStr=new String(dtTmp.getDate());
75
76 if (mStr.length==1){
77
78 mStr="0"+mStr;
79
80 }
81
82 if (dStr.length==1){
83
84 dStr="0"+dStr;
85
86 }
87 //這個日期年份,一定要用getFullYear(),這樣兼容所有瀏覽器。寫軟件的人員,一定要自己測試一下。否則,日期顯示無效 塗聚文注
88 return dtTmp.getFullYear()+"-"+mStr+"-"+dStr;
89
90 }
91
92 </script>
93
94 <script language="JavaScript">
95
96 function calculate(type)
97
98 {
99
100 var entry = document.getElementById(type).value;//自己判断是否合法
101
102 var uValue=document.form.u.value;
103
104 uValue=uValue.replace("-","/").replace("-","/");
105
106 //alert(uValue);
107
108
109//document.form.age1.value=age;
110
111 document.form.twice.value = DateAdd(type,entry,uValue);
112
113 }
114function strToDate(str)
115{
116 var val=Date.parse(str);
117 var newDate=new Date(val);
118 return newDate;
119}
120function strToDate1(str)
121{
122 var arys= new Array();
123 arys=str.split('-');
124 var newDate=new Date(arys[0],arys[1],arys[2]);
125 return newDate;
126}
127 function agedem()
128 {
129
130 var age,d,f,g,s,k,t,h;
131d=new Date();
132age=d.getFullYear();
133s=new Date();
134s=s.getUTCDay();
135document.form.age3.value=s;
136k=new Date();
137k=k.getDay();
138document.form.age4.value=k;
139g=new Date();
140f=g.getUTCFullYear();
141document.form.age2.value=f;
142t=document.form.u.value;
143//用以替换FireFox浏览器不兼容的日期格式 否則,日期顯示無效 涂聚文
144t=t.replace("-","/").replace("-","/");
145 //t=str.split('-');
146 // t=Date.parse(t);
147h=new Date(t)
148 // h=new Date(t[0],t[1],t[2]);
149
150 h=h.getFullYear();
151document.form.age5.value=h;
152//计算年份差
153document.form.age6.value=f-h
154//alert(f);
155}
156
157var nextYear = new Date(); // 初始化日期对象
158
159nextyear.setYear(nextYear.getYear() + 1); // 增加1年
160</script>
161<script type="text/javascript">
162
163<!--
164
165var m_names = new Array("January", "February", "March",
166"April", "May", "June", "July", "August", "September",
167"October", "November", "December");
168
169var d = new Date();
170var curr_date = d.getDate();
171var curr_month = d.getMonth();
172var curr_year = d.getFullYear();
173document.write(curr_date + "-" + m_names[curr_month]
174+ "-" + curr_year);
175
176/* The last two lines above have
177to placed on a single line */
178
179//-->
180</script>
181<html>
182<head>
183 <title>Javascript Convert String to Date</title>
184</head>
185<body>
186
187<script type="text/javascript">
188
189var dateString = "03-20-2008"; //因为不作条件考虑,不可以兼容
190 // var dateString = "03/20/2008"; mm/dd/yyyy [IE, FF]
191// You can also try the following Date string formats
192// var dateString = "2008/03/20"; // yyyy/mm/dd [IE, FF]
193// var dateString = "03-20-2008"; // mm-dd-yyyy [IE only]
194// var dateString = "March 20, 2008"; // mmmm dd, yyyy [IE, FF]
195// var dateString = "Mar 20, 2008"; // mmm dd, yyyy [IE, FF]
196
197// Initalize the Date object by passing the date string variable
198var myDate = new Date(dateString);
199
200// get Date part from the date string
201document.write("Date : " + myDate.getDate());
202document.write("<br>");
203
204// get Month part from the date string
205document.write("Month : " + myDate.getMonth());
206document.write("<br>");
207
208// get Full Year from the date string
209document.write("Year : " + myDate.getFullYear());
210</script>
211
212</body>
213</html>
214</head>
215
216<body bgcolor="#FFFFFF">
217<form name="form" action="" method="post"><TR>
218
219 <TD>要加的日期2:</TD>
220
221 <TD><input type="text" name="u" value="2005-03-20" onblur="agedem()" onclick="agedem()"></TD>
222
223 </TR>
224
225 <TR>
226
227 <TD>结果2:</TD>
228
229 <TD><input name="twice"><br><input name="age1"><input name="age2"/><input name="age3"/><input name="age4"/><input name="age5"/>日期差(年龄)<input name="age6"/></TD>
230
231 </TR>--> </TABLE>
232
233 </form>
234</body>
235</html>
2<html xmlns="http://www.w3.org/1999/xhtml" >
3<head>
4<title>javascript 操作日期 测试</title>
5<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6<META HTTP-EQUIV="REFRESH" CONTENT="11; URL=http://www.dusystem.com/index.htm"> 2006-03-12 designer Geovin Du-->
7<meta name="description" content="涂聚文,缔友计算机信息技术有限公司,经营信息流,物流,人力资源流,资本流的系统解决方案的开发与设计和服务,geoVI studio.Geovin Du. Systems Solution to Fund flow,and Information flow,and Material flow,and Control flow,and Human Resource.Applied software development,design and service。.">
8<meta name="description" content="Study,Communicate。 ">
9<meta name="Robots" content="all index follow ">
10<meta name="Author" content="涂聚文" />
11<link rel="shortcut icon" href="http://www.dusystem.com/favicon.ico" type="image/x-icon" />
12<link rel="icon" href="http://www.dusystem.com/favicon.ico" type="image/ico" />
13<link rel="Bookmark" href="http://www.dusystem.com/favicon.ico">
14 <script language="JavaScript">
15//此代碼,在IE6上不兼容,FireFox可以。
16 function DateAdd(strInterval, NumDay, dtDate) {
17
18 var dtTmp = new Date(dtDate);
19
20 if (isNaN(dtTmp)) dtTmp = new Date();
21
22 switch (strInterval) {
23
24 case "s":
25
26 dtTmp = new Date(Date.parse(dtTmp) + (1000 * parseInt(NumDay)));
27
28 break;
29
30 case "n":
31
32 dtTmp = new Date(Date.parse(dtTmp) + (60000 * parseInt(NumDay)));
33
34 break;
35
36 case "h":
37
38 dtTmp = new Date(Date.parse(dtTmp) + (3600000 * parseInt(NumDay)));
39
40 break;
41
42 case "d":
43
44 dtTmp = new Date(Date.parse(dtTmp) + (86400000 * parseInt(NumDay)));
45
46 break;
47
48 case "w":
49
50 dtTmp = new Date(Date.parse(dtTmp) + ((86400000 * 7) * parseInt(NumDay)));
51
52 break;
53
54 case "m":
55
56 dtTmp = new Date(dtTmp.getFullYear(), (dtTmp.getMonth())+parseInt(NumDay), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
57
58 break;
59
60 case "y":
61
62 //alert(dtTmp.getFullYear());
63
64 dtTmp = new Date(dtTmp.getFullYear()+parseInt(NumDay), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
65
66 //alert(dtTmp);
67
68 break;
69
70 }
71
72 var mStr=new String(dtTmp.getMonth()+1);
73
74 var dStr=new String(dtTmp.getDate());
75
76 if (mStr.length==1){
77
78 mStr="0"+mStr;
79
80 }
81
82 if (dStr.length==1){
83
84 dStr="0"+dStr;
85
86 }
87 //這個日期年份,一定要用getFullYear(),這樣兼容所有瀏覽器。寫軟件的人員,一定要自己測試一下。否則,日期顯示無效 塗聚文注
88 return dtTmp.getFullYear()+"-"+mStr+"-"+dStr;
89
90 }
91
92 </script>
93
94 <script language="JavaScript">
95
96 function calculate(type)
97
98 {
99
100 var entry = document.getElementById(type).value;//自己判断是否合法
101
102 var uValue=document.form.u.value;
103
104 uValue=uValue.replace("-","/").replace("-","/");
105
106 //alert(uValue);
107
108
109//document.form.age1.value=age;
110
111 document.form.twice.value = DateAdd(type,entry,uValue);
112
113 }
114function strToDate(str)
115{
116 var val=Date.parse(str);
117 var newDate=new Date(val);
118 return newDate;
119}
120function strToDate1(str)
121{
122 var arys= new Array();
123 arys=str.split('-');
124 var newDate=new Date(arys[0],arys[1],arys[2]);
125 return newDate;
126}
127 function agedem()
128 {
129
130 var age,d,f,g,s,k,t,h;
131d=new Date();
132age=d.getFullYear();
133s=new Date();
134s=s.getUTCDay();
135document.form.age3.value=s;
136k=new Date();
137k=k.getDay();
138document.form.age4.value=k;
139g=new Date();
140f=g.getUTCFullYear();
141document.form.age2.value=f;
142t=document.form.u.value;
143//用以替换FireFox浏览器不兼容的日期格式 否則,日期顯示無效 涂聚文
144t=t.replace("-","/").replace("-","/");
145 //t=str.split('-');
146 // t=Date.parse(t);
147h=new Date(t)
148 // h=new Date(t[0],t[1],t[2]);
149
150 h=h.getFullYear();
151document.form.age5.value=h;
152//计算年份差
153document.form.age6.value=f-h
154//alert(f);
155}
156
157var nextYear = new Date(); // 初始化日期对象
158
159nextyear.setYear(nextYear.getYear() + 1); // 增加1年
160</script>
161<script type="text/javascript">
162
163<!--
164
165var m_names = new Array("January", "February", "March",
166"April", "May", "June", "July", "August", "September",
167"October", "November", "December");
168
169var d = new Date();
170var curr_date = d.getDate();
171var curr_month = d.getMonth();
172var curr_year = d.getFullYear();
173document.write(curr_date + "-" + m_names[curr_month]
174+ "-" + curr_year);
175
176/* The last two lines above have
177to placed on a single line */
178
179//-->
180</script>
181<html>
182<head>
183 <title>Javascript Convert String to Date</title>
184</head>
185<body>
186
187<script type="text/javascript">
188
189var dateString = "03-20-2008"; //因为不作条件考虑,不可以兼容
190 // var dateString = "03/20/2008"; mm/dd/yyyy [IE, FF]
191// You can also try the following Date string formats
192// var dateString = "2008/03/20"; // yyyy/mm/dd [IE, FF]
193// var dateString = "03-20-2008"; // mm-dd-yyyy [IE only]
194// var dateString = "March 20, 2008"; // mmmm dd, yyyy [IE, FF]
195// var dateString = "Mar 20, 2008"; // mmm dd, yyyy [IE, FF]
196
197// Initalize the Date object by passing the date string variable
198var myDate = new Date(dateString);
199
200// get Date part from the date string
201document.write("Date : " + myDate.getDate());
202document.write("<br>");
203
204// get Month part from the date string
205document.write("Month : " + myDate.getMonth());
206document.write("<br>");
207
208// get Full Year from the date string
209document.write("Year : " + myDate.getFullYear());
210</script>
211
212</body>
213</html>
214</head>
215
216<body bgcolor="#FFFFFF">
217<form name="form" action="" method="post"><TR>
218
219 <TD>要加的日期2:</TD>
220
221 <TD><input type="text" name="u" value="2005-03-20" onblur="agedem()" onclick="agedem()"></TD>
222
223 </TR>
224
225 <TR>
226
227 <TD>结果2:</TD>
228
229 <TD><input name="twice"><br><input name="age1"><input name="age2"/><input name="age3"/><input name="age4"/><input name="age5"/>日期差(年龄)<input name="age6"/></TD>
230
231 </TR>--> </TABLE>
232
233 </form>
234</body>
235</html>
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)