2 回答

TA貢獻1796條經驗 獲得超4個贊
你只需要改變你while的檢查是否date.getDay()是!== 0:
while (date.getMonth() == month) {
//Getting all days from here and storing in array named as all)days();
if (date.getDay() === 0){
continue;
}
var d = date.getFullYear() + '-' + my_month.toString().padStart(2, '') + '-' + date.getDate().toString().padStart(2, '0');
all_days.push(d);
date.setDate(date.getDate() + 1);
//$('.test').append(all_days);
}

TA貢獻1802條經驗 獲得超5個贊
我喜歡這個函數getDaysInMonth它很容易理解,所以我擴展了它
/**
*
* @param {int} The month number, 0 based
* @param {int} The year, not zero based, required to account for leap years
* @param {Array} Which days to exclude Sunday is 0, Monday is 1, and so on.
* @returns {Date[]} List with date objects
*/
function getDaysInMonthWithExclude(month, year, excludeweekdays) {
var date = new Date(Date.UTC(year, month, 1));
var days = [];
while (date.getMonth() === month) {
if (excludeweekdays.indexOf(date.getDay()) === -1) {
days.push(new Date(date));
}
date.setDate(date.getDate() + 1);
}
return days;
}
console.log(getDaysInMonthWithExclude(8, 2019, [0]));
添加回答
舉報