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

求给定一个日期,算出该日期在那个月第几周的函数,用JS
求给定一个日期,算出该日期在那个月第几周的函数,用JS
哪位大虾指点指点...

------解决方案--------------------
<SCRIPT LANGUAGE= "JavaScript ">
<!--
Date.prototype.getWeek = function(flag)
{
var first = new Date(this.getFullYear(), 0, 1);
var n = parseInt( "1065432 ".charAt(first.getDay()));
n = this.getTime()-first.getTime()-n*24*60*60*1000;
n = Math.ceil(n/(7*24*60*60*1000));
return (flag==true&&first.getDay()!=1)?(n+1):n;
};
alert( "今天是今年的第 "+ new Date().getWeek() + " 周\n本年第一周周一在上一年的情况 ")
alert( "今天是今年的第 "+ new Date().getWeek(true) + " 周 ")
alert( "2006/10/1 是第 "+ new Date( "2006/10/1 ").getWeek(true) + " 周 ")
//-->
</SCRIPT>
------解决方案--------------------
更新一下..
JScript code

<script type="text/javascript">
var getMonthWeek = function (a, b, c) {
/*
a = d = 当前日期
b = 6 - w = 当前周的还有几天过完(不算今天)
a + b 的和在除以7 就是当天是当前月份的第几周
*/
    var date = new Date(a, parseInt(b) - 1, c), w = date.getDay(), d = date.getDate();
    return Math.ceil(
        (d + 6 - w) / 7
    );
};

var getYearWeek = function (a, b, c) {
/*
date1是当前日期
date2是当年第一天
d是当前日期是今年第多少天
用d + 当前年的第一天的周差距的和在除以7就是本年第几周
*/
    var date1 = new Date(a, parseInt(b) - 1, c), date2 = new Date(a, 0, 1),
        d = Math.round((date1.valueOf() - date2.valueOf()) / 86400000);
    return Math.ceil(
        (d + ((date2.getDay() + 1) - 1)) / 7
    );
};

document.write(
    "今天是本月的第 ", getMonthWeek(2007, 10, 17), " 周<br \/>"
    , "今天是本年的第 ", getYearWeek(2007, 10, 17), " 周"
);
</script>