获取当月所有周的所有日子 - javascript / Jquery

时间:2012-07-05 05:33:46

标签: javascript jquery

我需要一个代码来获取当月所有星期的所有日子,

一个函数,它接受一周的数字作为参数并返回该特定周的所有日期。

代码必须是javascript或jquery,

有人可以帮帮我吗?

提前致谢:)

问候。

1 个答案:

答案 0 :(得分:1)

以下是使用java脚本获取当月特定周的所有工作日的完整解决方案。

按照步骤:

1)HTML:

<div class="input">
  <label>
    No. of Weeks..?: 
  </label>
  <input type="text" size="3" id="txtweek" name="txtweek" />
  <input type="button" id="btnok" name="btnok" value="Get Week Days" onclick="getWeekDays();" />
</div>
<div id="result">
</div>

2)CSS:

.input label{
  font-size:13px;
  font-weight:bold;
}
.input input{
  font-size:13px;
  border:1px solid #333;
}
.weekdays{
  width:40%;
  background-color:#11a8a5;
  box-shadow: 7px 7px 3px #446689;
  margin-top:5px;
}
.weekdays th{
  background-color:#885511;
  border-bottom:1px solid #a4a525;
  color:#ffad25;
}
.weekdays td{
  text-align:center;
}

3)JAVA SCRIPT:

 var startDay = 0; //0=sunday, 1=monday etc.
var week = 0;
var result = "";

function weekDaysInMonth(week, startDay) {

    if (startDay == "" && startDay == null && typeof(startDay) == "undefined") {
        startDay = 0;
    }
    if (week != "" && week != null && typeof(week) != "undefined") {

        var weekDay = new Array(7);
        weekDay[0] = "Sunday";
        weekDay[1] = "Monday";
        weekDay[2] = "Tuesday";
        weekDay[3] = "Wednesday";
        weekDay[4] = "Thursday";
        weekDay[5] = "Friday";
        weekDay[6] = "Saturday";

        var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

        var currDate = new Date();
        var currWeekDay = week * 7;
        var monthLastDay = new Date(currDate.getFullYear(), currDate.getMonth() + 1, 0).getDate();

        if (currWeekDay > monthLastDay) {
            currWeekDay = monthLastDay;
            currDate = new Date(currDate.getFullYear(), currDate.getMonth() + 1, 0);

        } else {
            currDate = new Date((currDate.getMonth() + 1) + '/' + currWeekDay + '/' + currDate.getFullYear());
        }

        var d = currDate.getDay(); //get the current day
        var weekStart = new Date(currDate.valueOf() - (d <= 0 ? 7 - startDay : d - startDay) * 86400000); //rewind to start day
        var weekEnd = new Date(weekStart.valueOf() + 7 * 86400000);

        var wkDay = weekStart.getDate(); //Start week Date
        var nextDate = weekStart; //Assign Next Date as Current Week Start Date
        var htmlWeekDays = "",
            nextDay = "";
        while (wkDay != weekEnd.getDate()) {
            nextDay = nextDate.getDay();
            htmlWeekDays += "<tr><td>" + nextDate.getDate() + " " + monthNames[nextDate.getMonth()] + "," + nextDate.getFullYear() + "</td><td>" + weekDay[nextDay] + "</td></tr>";
            nextDate = new Date(nextDate.getTime() + 86400000);
            wkDay = nextDate.getDate();

        }
        if (htmlWeekDays != "") {
            htmlWeekDays = "<table cellpedding=0 cellspacing=0 class='weekdays'><tr><th>Date</th><th>Day</th></tr>" + htmlWeekDays + "</table>";
        }
        return htmlWeekDays;
    } else {
        return false;
    }


    return new Date(year, month, 0).getDate();
}

function getWeekDays() {
    week = document.getElementById("txtweek").value.trim();
    if (week != "" && !isNaN(week)) {
        result = weekDaysInMonth(week, startDay);
        document.getElementById("result").innerHTML = result;
    }
}

上面的脚本只接受一周中的no,并使用动态生成的表格格式获取该周的所有日期。

我还在http://codebins.com/bin/4ldqpbi

上生成了bin演示