1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2 <HTML>
3 <HEAD>
4 <TITLE> 作者:风中叶 </TITLE>
5 <META NAME="Generator" CONTENT="EditPlus">
6 <META NAME="Author" CONTENT="">
7 <META NAME="Keywords" CONTENT="">
8 <META NAME="Description" CONTENT="">
9
10 <SCRIPT LANGUAGE="JavaScript">
11 //将string转换为Date
12 //str必须满足如下格式: 2008-04-29
13 function convertString2Date(str)
14 {
15 //用户split分隔出数组,将包含3个元素:年,月,日
16 var splitArray = str.split("-");
17
18 // 用年,月,日构造日期对象
19 // splitArray[0]-> year, splitArray[1]-> month, splitArray[2]->day
20 // 这时要注意月份是从0开始的
21 var date = new Date(splitArray[0], splitArray[1] - 1, splitArray[2]);
22
23 return date;
24 }
25
26 // 得到date1和date2之间的间隔
27 // date2要比date1大
28 // date要满足如下格式: 2008-04-29
29 //参数date1和date2类型应该是String或Date
30 // 确保两种类型没有问题
31 function getIntervalBetweenTwoDates(date1, date2)
32 {
33 var realDate1 = date1;
34 var realDate2 = date2;
35
36 // 如果date1是Date类型就不用转换
37 if(!(date1 instanceof Date))
38 {
39 realDate1 = convertString2Date(date1);
40 }
41 // 如果date2是Date类型就不用转换
42 if(!(date2 instanceof Date))
43 {
44 realDate2 = convertString2Date(date2);
45 }
46
47 //得到绝对值,(realDate2.getTime() - realDate1.getTime())返回的是毫秒所以要先除1000
48 var interval = Math.abs((realDate2.getTime() - realDate1.getTime()))/(1000 * 60 * 60 * 24);
49
50 return interval;
51 }
52
53 function listAllDatesBetweenTwoDates()
54 {
55 var date1 = document.getElementById("start").value;
56 var date2 = document.getElementById("end").value;
57 var target = document.getElementById("result");
58 var interval = getIntervalBetweenTwoDates(date1,date2);
59 // 转换string为Date
60 date1 = convertString2Date(date1);
61 var year = date1.getFullYear();
62 var month = date1.getMonth();
63 var date = date1.getDate();
64 --date;
65
66 for(var i = 0 ; i <= interval ; ++i)
67 {
68 ++date;
69
70 var theDate = new Date(year, month, date);
71 var tempYear = theDate.getFullYear();
72 var tempMonth = theDate.getMonth() + 1;
73 var tempDate = theDate.getDate();
74 var day = theDate.getDay();
75 var working = null;
76 if(0 == day || 6 == day) // non-working day
77 {
78 working = "non-working day";
79 }
80 else
81 {
82 working = "working day";
83 }
84 var theResult = tempYear + "-" + tempMonth + "-" + tempDate + " is " + working;
85 var div = document.createElement("div");
86
87 div.innerHTML = theResult;
88 target.appendChild(div);
89
90 }
91 }
92
93 </SCRIPT>
94 </HEAD>
95 <BODY>
96
97 start date:<input type="text" length="20" id="start"><br>
98 end date:<input type="text" length="20" id="end"><br>
99 <input type="button" onclick="listAllDatesBetweenTwoDates()" value="result">
100 <div id="result"></div>
101 </BODY>
102 </HTML>
103
posted @
2008-07-02 09:38 onlyfish 阅读(430) |
评论 (0) |
编辑 收藏