javascript 获取某一日期属于一年中那一周及这周开始和结束日期

 多也不说了,直接贴代码,代码是经过测试没发现问题!

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<script type="text/javascript">
    function weekofday(data) {//判断某一日属于这一年的第几周
        debugger;
        var dt = new Date(data)
        var y = dt.getFullYear()
        var start = "1/1/" + y
        start = new Date(start)
        starts = start.valueOf()
        startweek = start.getDay()
        dtweek = dt.getDay()
        var days = Math.round((dt.valueOf() - start.valueOf()) / (24 * 60 * 60 * 1000)) - (7 - startweek) - dt.getDay() - 1
        days = Math.floor(days / 7)
        return (days + 2);
    }
 function show() {

      var time="2010-04-28";
      oToday = new Date(time);
      currentDay = oToday.getDay();
      if (currentDay == 0) { currentDay = 7 }
      mondayTime = oToday.getTime() - (currentDay - 1) * 24 * 60 * 60 * 1000;
      sundayTime = oToday.getTime() + (7 - currentDay) * 24 * 60 * 60 * 1000;
      alert(weekofday(time));
      alert("今天是" + oToday.getDate() + "号,星期" + currentDay + "\r");
      alert("周一是" + new Date(mondayTime).getDate() + "号,周日是" + new Date(sundayTime).getDate() + "号")
      alert("周一 " + new Date(mondayTime).toLocaleDateString() + "\n周末 " + new Date(sundayTime).toLocaleDateString() + " " + new Date(mondayTime).toLocaleTimeString())
    }
</script>

</head>
<body>

<input name="Submit" type="image" value="提交" onclick="show()" src="http://images.cnblogs.com/pass/pass_01.gif" align="absmiddle" />

</body>

</html>

posted @ 2010-04-28 10:27  Bug从零开始  阅读(1619)  评论(1编辑  收藏  举报