我正在尝试document.write周数,然后写那周的天数。例如,如果您输入2016年4月作为您想要的月份,则会显示weekNum,然后在第1周的第一个是星期五,第二个是星期六。然后第二周的第三天是星期天,第四天是星期一,依此类推。

//variables
var reqMon;
var todaysDate = new Date();
var currYr = todaysDate.getFullYear();
var currMon = todaysDate.getMonth();
var currDate = todaysDate.getDate();
var currDay = todaysDate.getDay();

//creates a month array.
var monthArray = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
//creates a day array.
var weekDaysArray = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
//creates an array for how many days in each month.
var maxMonthlyDays = new Array("31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31");

//prompts user for numeric month.
var reqMon = parseInt(prompt("Enter desired month (Jan = 1, Dec = 12)"));

//validates users month input.
while (reqMon == "" || reqMon < 1 || reqMon > 12) {
    reqMon = prompt("Invalid Entry. Please enter the month number again.")
}

//Step 3B.
document.write(reqMon + "/" + currDate + "/" + currYr + "<br /><br />");

//Step 4.
document.write(monthArray[reqMon -1] + " " + currYr + "<br /><br />");

var dateComb = (reqMon + "/1/" + currYr);
var userIdate = new Date(dateComb);
var userDay = new Date(dateComb).getDay();
var userDate = new Date(dateComb).getDate();
var maxDays = maxMonthlyDays[reqMon -1];
var numberWeeks = maxMonthlyDays[reqMon - 1] / 6;

// ****************  STEP 5 **************.
//loops through weeks of month.
for (x = 1; x <= maxMonthlyDays[reqMon] / 6; x++) {
    document.write("<b>Week " + x + "<br /></b>");

    while (userDay <= 6 && userDate <= maxMonthlyDays[reqMon]) {
        document.write(weekDaysArray[userDay] + " " + userDate + "<br />");
        userDay++;
        userDate++;
    }
}


我可以获取weekNum进行迭代并输出到屏幕,我可以获取所需月份中进行迭代和输出的总天数。但是我似乎无法“仅在适当的weekNum下输出1,2,3等天。

提前致谢。

最佳答案

试试这个,我不确定您到底需要什么。

    for (x = 1; x <= numberWeeks; x++) {
        document.write("<b>Week " + x + "<br /></b>");
        while (userDay <= 6 && userDate <= maxDays) {
            document.write(weekDaysArray[userDay] + " " + userDate + "<br />");
            userDay++;
            userDate++;
        }
        userDay = 0;
    }

10-08 00:16