forked from AlloyTeam/alloyteam.github.com
-
Notifications
You must be signed in to change notification settings - Fork 4
/
pp.html
146 lines (114 loc) · 3.49 KB
/
pp.html
1
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>P仔</title>
<style>
body{
font: 14px/1.8 Tahoma;
}
</style>
</head>
<body>
P 仔<br />
<input type="text" length="4" id="year" value="2013"><br>
start M:<input type="text" length="2" id="start" value="2"><br>
end M:<input type="text" length="2" id="end" value="3"><br>
<input type="button" onclick="listAllDatesBetweenTwoDates()" value="result">
<div id="result"></div>
<script>
//将string转换为Date
//str必须满足如下格式: 2008-04-29
function convertString2Date(str)
{
//用户split分隔出数组,将包含3个元素:年,月,日
var splitArray = str.split("-");
// 用年,月,日构造日期对象
// splitArray[0]-> year, splitArray[1]-> month, splitArray[2]->day
// 这时要注意月份是从0开始的
var date = new Date(splitArray[0], splitArray[1] - 1, splitArray[2]);
return date;
}
// 得到date1和date2之间的间隔
// date2要比date1大
// date要满足如下格式: 2008-04-29
//参数date1和date2类型应该是String或Date
// 确保两种类型没有问题
function getIntervalBetweenTwoDates(date1, date2)
{
var realDate1 = date1;
var realDate2 = date2;
// 如果date1是Date类型就不用转换
if(!(date1 instanceof Date))
{
realDate1 = convertString2Date(date1);
}
// 如果date2是Date类型就不用转换
if(!(date2 instanceof Date))
{
realDate2 = convertString2Date(date2);
}
//得到绝对值,(realDate2.getTime() - realDate1.getTime())返回的是毫秒所以要先除1000
var interval = Math.abs((realDate2.getTime() - realDate1.getTime()))/(1000 * 60 * 60 * 24);
return interval;
}
var $ = function(id){
return document.getElementById(id);
}
function listAllDatesBetweenTwoDates()
{
var year = $("year").value;
var monthStart = $("start").value;
var monthEnd = $("end").value;
var date1 = year +"-"+ monthStart + "-01";
var date2 = year +"-"+ monthEnd + "-31";
var target = $("result");
var interval = getIntervalBetweenTwoDates(date1,date2);
// 转换string为Date
date1 = convertString2Date(date1);
var year = date1.getFullYear();
var month = date1.getMonth();
var date = date1.getDate();
--date;
for(var i = 0 ; i <= interval ; ++i)
{
++date;
var theDate = new Date(year, month, date);
var tempYear = theDate.getFullYear();
var tempMonth = theDate.getMonth() + 1;
var tempDate = theDate.getDate();
var day = theDate.getDay();
var theDay = theDate.getTime()/1000/60/60/24;
var thatDay = new Date(2012, 09, 19).getTime()/1000/60/60/24;
var working = "";
if(0 == day || 6 == day) // non-working day
{
working += "周末";
}
else
{
working += "周 "+day;
}
if((theDay-thatDay)%3!=0) // non-working day
{
working += "";
}
else
{
working += " 【Working...】";
}
var theResult = tempYear + "-" + tempMonth + "-" + tempDate + ": " + working;
var div = document.createElement("div");
div.innerHTML = theResult;
target.appendChild(div);
}
}
var start = function(){
$("start").value = (new Date()).getMonth()+1;
$("end").value = Number($("start").value)+2;
listAllDatesBetweenTwoDates();
};
start();
</script>
</body>
</html>