|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> 作者:风中叶 </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<SCRIPT LANGUAGE="JavaScript">
//将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;
}
function listAllDatesBetweenTwoDates()
{
var date1 = document.getElementById("start").value;
var date2 = document.getElementById("end").value;
var target = document.getElementById("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 working = null;
if(0 == day || 6 == day) // non-working day
{
working = "non-working day";
}
else
{
working = "working day";
}
var theResult = tempYear + "-" + tempMonth + "-" + tempDate + " is " + working;
var div = document.createElement("div");
div.innerHTML = theResult;
target.appendChild(div);
}
}
</SCRIPT>
</HEAD>
<BODY>
start date:<input type="text" length="20" id="start"><br>
end date:<input type="text" length="20" id="end"><br>
<input type="button" onclick="listAllDatesBetweenTwoDates()" value="result">
<div id="result"></div>
</BODY>
</HTML>
|