如何使用Javascript查找一个月内的周数

时间:2016-12-18 11:42:02

标签: javascript

如何使用Javascript查找一个月内的周数?

我使用了以下代码并给出了错误答案。

我在以下代码中传递月份和年份。

var firstOfMonth = new Date(year, month - 1, 1);
var lastOfMonth = new Date(year, month, 0);
var used = firstOfMonth.getDay() + lastOfMonth.getDate();
var lastweek = Math.ceil(used / 7);

2 个答案:

答案 0 :(得分:0)

function week(year, month-number) {

    var firstOfMonth = new Date(year, month_number-1, 1);
    var lastOfMonth = new Date(year, month_number, 0);

    var used = firstOfMonth.getDay() + lastOfMonth.getDate();

    return Math.ceil( used / 7);
}

function weekDay() {

    var date = new Date,
        days = ['Sunday','Monday','Tuesday','Wednesday',
                'Thursday','Friday','Saturday'],
        prefixes = ['First', 'Second', 'Third', 'Fourth', 'Fifth'];

    return prefixes[0 | date.getDate() / 7] + ' ' + days[date.getDay()];

}
weekDay(); 

答案 1 :(得分:0)

function week(year, monthNumber) {

    var firstOfMonth = new Date(year, monthNumber, 1);
    var lastOfMonth = new Date(year, monthNumber + 1, 0);

    var used = firstOfMonth.getDay() + lastOfMonth.getDate();

    return Math.ceil( used / 7);
}