如何获得一个月中不包括周末的天数
How to get number of days in a month excluding weekends
我已经编写了一个函数来提供一个月中除周末外的所有天数。一切正常,但是当我想获得十二月的日期时,下周的日期对象 returns 1 月 1 日和函数 returns 一个空数组。
请帮忙。
function getDaysInMonth(month, year) {
var date = new Date(year, month-1, 1);
var days = [];
while (date.getMonth() === month) {
// Exclude weekends
var tmpDate = new Date(date);
var weekDay = tmpDate.getDay(); // week day
var day = tmpDate.getDate(); // day
if (weekDay !== 1 && weekDay !== 2) {
days.push(day);
}
date.setDate(date.getDate() + 1);
}
return days;
}
alert(getDaysInMonth(month, year))
创建日期时,您使用月份 = 0 到 11
这也适用于 GET 月份 - 它也是 returns 0 到 11
我很惊讶你这么说
Every thing works fine
它实际上从来没有工作过任何一个月 - 总是 return 一个空数组
function getDaysInMonth(month, year) {
month--; // lets fix the month once, here and be done with it
var date = new Date(year, month, 1);
var days = [];
while (date.getMonth() === month) {
// Exclude weekends
var tmpDate = new Date(date);
var weekDay = tmpDate.getDay(); // week day
var day = tmpDate.getDate(); // day
if (weekDay%6) { // exclude 0=Sunday and 6=Saturday
days.push(day);
}
date.setDate(date.getDate() + 1);
}
return days;
}
alert(getDaysInMonth(month, year))
我已经编写了一个函数来提供一个月中除周末外的所有天数。一切正常,但是当我想获得十二月的日期时,下周的日期对象 returns 1 月 1 日和函数 returns 一个空数组。
请帮忙。
function getDaysInMonth(month, year) {
var date = new Date(year, month-1, 1);
var days = [];
while (date.getMonth() === month) {
// Exclude weekends
var tmpDate = new Date(date);
var weekDay = tmpDate.getDay(); // week day
var day = tmpDate.getDate(); // day
if (weekDay !== 1 && weekDay !== 2) {
days.push(day);
}
date.setDate(date.getDate() + 1);
}
return days;
}
alert(getDaysInMonth(month, year))
创建日期时,您使用月份 = 0 到 11
这也适用于 GET 月份 - 它也是 returns 0 到 11
我很惊讶你这么说
Every thing works fine
它实际上从来没有工作过任何一个月 - 总是 return 一个空数组
function getDaysInMonth(month, year) {
month--; // lets fix the month once, here and be done with it
var date = new Date(year, month, 1);
var days = [];
while (date.getMonth() === month) {
// Exclude weekends
var tmpDate = new Date(date);
var weekDay = tmpDate.getDay(); // week day
var day = tmpDate.getDate(); // day
if (weekDay%6) { // exclude 0=Sunday and 6=Saturday
days.push(day);
}
date.setDate(date.getDate() + 1);
}
return days;
}
alert(getDaysInMonth(month, year))