javascript - javascript - 使用Date对象查找一个月内的所有日期?

有没有办法用日期对象来查找天? 我找到了这个链接,例如: 显示月份的所有日期,但是,我不太理解它,而且它是Java写的,我正在试图在javascript中找到一个解决方案。

时间:

若要获得每月的列表,可以在每月的第一天以Date开头,增加月份的日期。


/**


 * @param {int} The month number, 0 based


 * @param {int} The year, not zero based, required to account for leap years


 * @return {Date[]} List with date objects for each day of the month


 */


function getDaysInMonth(month, year) {


 var date = new Date(year, month, 1);


 var days = [];


 while (date.getMonth() === month) {


 days.push(new Date(date));


 date.setDate(date.getDate() + 1);


 }


 return days;


}



通过执行以下操作,可以很容易地构建一个月的数组:


var numOfDays = new Date(2012, 10, 0).getDate(); //use 0 here and the actual month


var days = new Array();



//This will construct an array with all the elements represent the day of the week 


//(i.e. Oct 30th would be days[30-1] or days[29]) and the value would be the actual 


//day of the week (i.e. Tuesday which is representing by the number 2)


for(var i=0;i<=numOfDays;i++)


{


 days[i] = new Date(2012,9,i+1).getDay(); //use month-1 here 


}


//This will give you a number from 0 - 6 which represents (Sunday - Saturday)


alert(days[29]); 



使用这个数组,你可以用它做你想要的事情,并知道一周的日期。

...