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

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

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

例如:

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

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

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


提前感谢。

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

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

例如:

var datesOfWeek=[];
var workingDate=时刻(新日期(年,1,1));
工作日期。添加(“周”,周);
for(var dayOfWeek=0;dayOfWeek<7;dayOfWeek++){
工作日期。天(星期五);
datesOfWeek.push(workingDate.toDate());
}

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

函数getDatesByWeekNumber(年、月、周数、第一个工作日){ var-ret=[]; 第一个工作日=第一个工作日| | 0; var lastWeekDay=7+firstWeekDay; var tempDate=新日期(年、月、1); console.log(“tempDate:+tempDate”); var DaysInThis month=daysInMonth(年,月); console.log(“daysinthis month:+daysinthis month”); log(“最后一个工作日:”+(7+第一个工作日)); //查找一周中的第一天以查找 var curMonth=tempDate.getMonth(); var curYear=tempDate.getFullYear(); var weekCounter=0; 而(weekCounter而((dayCounter<7)和&(dayPerWeekCountervar 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));