日期:2014-05-16  浏览次数:20538 次

求助:计算星期几是几号的函数
因为项目中加入日历的缘故,要在日程安排的时候加入,选择每周1,2,3等定时提醒,现在需要在用户选择周一时,能计算出所在年的所有客户选择的星期几对应的日期,比如客户选择每周一提醒,则能够计算出2007年所有星期一的日期.
多谢各位大虾,我会给分

------解决方案--------------------
LZ 五一长假还不忘工作,佩服佩服!

写了一小段,LZ 看看行不,不行就自己动手改改,L@_@K!

<body>
<input type= "text " id= "txtYear " value= "2007 " />
<select id= "selDays ">
<option value= "-1 " selected> -- 请选择 -- </option>
<option value= "0 "> 星期天 </option>
<option value= "1 "> 星期一 </option>
<option value= "2 "> 星期二 </option>
<option value= "3 "> 星期三 </option>
<option value= "4 "> 星期四 </option>
<option value= "5 "> 星期五 </option>
<option value= "6 "> 星期六 </option>
</select>
<input type= "button " id= "btnCalculate " value= "计算 " />
<div id= "divResult "> </div>
<script type= "text/javascript ">
<!--
var oYear = document.getElementById( "txtYear ");
var oDays = document.getElementById( "selDays ");
var oCalculate = document.getElementById( "btnCalculate ");
var oResult = document.getElementById( "divResult ");

Date.prototype.getY4M2D2 = function()
{
var strDelimiter = "- ";
var numMonth = this.getMonth()+1;
var numDate = this.getDate();
var strY4M2D2 = this.getFullYear() + strDelimiter;
strY4M2D2 += (numMonth <10 ? ( "0 "+numMonth) : numMonth) + strDelimiter;
strY4M2D2 += numDate <10 ? ( "0 "+numDate) : numDate;
return strY4M2D2;
};

oCalculate.onclick = function()
{
var numYear = parseInt(oYear.value);
var numDay = parseInt(oDays.value);
if (numDay != -1)
{
// 月份,是从 0 到 11 之间的整数(1 月至 12 月)。
var datFirstDate = new Date(numYear, 0, 1);
var numFirstDay = datFirstDate.getDay();

var numInterval = numDay - numFirstDay;
if (numInterval < 0) numInterval += 7;

var numMillionsecondsPerDate = 1000*3600*24;
var datValidatedDay = new Date(datFirstDate.valueOf() + numMillionsecondsPerDate*numInterval);

var arrValidatedDays = new Array();
do
{
arrValidatedDays.push(datValidatedDay.getY4M2D2());
datValidatedDay = new Date(datValidatedDay.valueOf() + numMillionsecondsPerDate*7);
}
while (datValidatedDay.getFullYear() == numYear);

oResult.innerHTML = "共计 " + arrValidatedDays.length + " 天,详细列表如下: <br /> ";
oResult.innerHTML += arrValidatedDays.join( " <br /> ");
}
else
{
alert( "搞什么搞?快选! ");
}
};
//-->
</script>
</body>