在javascript中显示一周或一个月的日期?

在javascript中显示一周或一个月的日期?,javascript,jquery,Javascript,Jquery,如何使用javascript获取一周的天数和一个月的天数 例如: 周数、月和年将由用户提供 比如说2周,12个月和2012年 现在我想要结果日期为3,4,5,6,7,8,9 提前感谢。在使用JavaScript处理日期时,使用库为您完成一些繁重的工作通常是很方便的-例如矩.js: 使用moment.js,对于您的场景,您可以在年初创建一个您想要的时刻。然后添加与所需周相对应的周数。然后,您可以循环设置一周中的某一天太阳、周一等,并在循环过程中将一周中每一天的日期缓存到列表中 例如: 既然你决定重

如何使用javascript获取一周的天数和一个月的天数

例如:

周数、月和年将由用户提供

比如说2周,12个月和2012年

现在我想要结果日期为3,4,5,6,7,8,9


提前感谢。

在使用JavaScript处理日期时,使用库为您完成一些繁重的工作通常是很方便的-例如矩.js:

使用moment.js,对于您的场景,您可以在年初创建一个您想要的时刻。然后添加与所需周相对应的周数。然后,您可以循环设置一周中的某一天太阳、周一等,并在循环过程中将一周中每一天的日期缓存到列表中

例如:


既然你决定重新定义一周的开始,你让它变得更难,但既然你问了这个问题,我很想弄明白。以下是我的想法:

我相信有更好的方法来做我所做的事情,所以不要对我大喊大叫。我想弄明白这件事,脑子都快炸坏了。抱歉,如果变量名很奇怪-有很多i、j和k,所以我认为最好不要使用它们…也许会是这样。我尽了最大的努力测试了很多场景,我没有发现任何不起作用的东西,但我确信有些东西会出错

需要注意的几点:

我决定遵循Javascript日期约定——月从0开始,周从0开始,月从1开始。所以如果你想要12月,你可以在11月通过

就像我说的,你已经重新定义了一周的开始,所以我通过使用第四个参数0来定制它。例如,如果星期一是一周的第一天,就像它似乎与你在一起一样,你会使用1。它是可选的,如果省略它或传递错误的值,则默认为0

第三个参数是您要查找的月份的周数,从1开始。例如,如果你正在寻找第三周,你会使用3

它有安全检查,以确保它不会在月外的几天返回,以防你看到的一周是28、29、30、31…1、2、3-它不会抓住最后3天。同样,在这个月初的情况下,这个月的第一天是在一周中,你想要第一周。
因此,它将返回最多的项目是7个完整的,有效的一周。但是返回的次数很可能会少。

在JavaScript中处理日期时,使用库为您完成一些繁重的工作通常是很方便的—例如,矩.js:with the Moment.js,对于您的场景,您可以从您想要的年初创建一个时刻开始。然后添加与所需周相对应的周数。然后,您可以循环设置一周中的某一天太阳、周一等,并在循环过程中将一周中每一天的日期缓存到列表中。。。希望您能从编写一些代码开始。您对周数的定义是什么?我会说十二月的第二周从8号开始。。。
var datesOfWeek = [];
var workingDate = moment(new Date(year, 1, 1));
workingDate.add('weeks', week);
for(var dayOfWeek = 0; dayOfWeek < 7; dayOfWeek++) {
    workingDate.day(dayOfWeek);
    datesOfWeek.push(workingDate.toDate());
}
function getDatesByWeekNumber(year, month, weekNum, firstWeekDay) {
    var ret = [];

    firstWeekDay = firstWeekDay || 0;
    var lastWeekDay = 7 + firstWeekDay;

    var tempDate = new Date(year, month, 1);
    console.log("tempDate: " + tempDate);

    var daysInThisMonth = daysInMonth(year, month);
    console.log("daysInThisMonth : " + daysInThisMonth);

    console.log("lastWeekDay: " + (7 + firstWeekDay));

    // Finds the first day of the week looking for
    var curMonth = tempDate.getMonth();
    var curYear = tempDate.getFullYear();
    var weekCounter = 0;
    while (weekCounter < weekNum-1 && (curMonth === month && curYear === year)) {
        var dayCounter = 0;
        var dayPerWeekCounter = tempDate.getDay();

        //   No more than 7 days     No more than virtual last day                   Same month/year
        while ((dayCounter < 7) && (dayPerWeekCounter < lastWeekDay) && (curMonth === month && curYear === year)) {
            tempDate.setDate(tempDate.getDate() + 1);
            dayPerWeekCounter++;
            dayCounter++;
            curMonth = tempDate.getMonth();
            curYear = tempDate.getFullYear();
        }

        curMonth = tempDate.getMonth();
        curYear = tempDate.getFullYear();
    }

    console.log("First day of found week: " + tempDate);

    if (tempDate.getMonth() === month && tempDate.getFullYear() === year) {
        // Finds each day in week specified that doesn't go out of bounds
        var dayCounter = 0;
        var dayPerWeekCounter = tempDate.getDay();
        var dayPerMonthCounter = tempDate.getDate();
        //   No more than 7 days     No more than virtual last day            Not past end of month
        while ((dayCounter < 7) && (dayPerWeekCounter < lastWeekDay) && (dayPerMonthCounter <= daysInThisMonth)) {
            var cur = tempDate.getDate();
            ret.push(cur);
            tempDate.setDate(cur + 1);
            dayCounter++;
            dayPerWeekCounter++;
            dayPerMonthCounter++;
        }
    }

    return ret;
}

function copyDate(orig) {
    return new Date(orig.getTime());
}

function daysInMonth(year, month) {
    return new Date(year, month+1, 0).getDate();
}

var the_dates = getDatesByWeekNumber(2012, 11, 2, 1);
console.log("########FINAL ANSWER: " + JSON.stringify(the_dates));