var d = new Date();
var getTot = daysInMonth(d.getMonth(),d.getFullYear()); //Get total days in a month
var sat = new Array(); //Declaring array for inserting Saturdays
var sun = new Array(); //Declaring array for inserting Sundays
for(var i=1;i<=getTot;i++){ //looping through days in month
var newDate = new Date(d.getFullYear(),d.getMonth(),i)
if(newDate.getDay()==0){ //if Sunday
sun.push(i);
}
if(newDate.getDay()==6){ //if Saturday
sat.push(i);
}
}
console.log(sat);
console.log(sun);
function daysInMonth(month,year) {
return new Date(year, month, 0).getDate();
}
var my_date = "2014-01-07".split('-')
var year = parseInt(my_date[0]);
var month = parseInt(my_date[1])-1;
var saturdays = [];
var sundays = [];
for (var i = 0; i <= new Date(year, month, 0).getDate(); i++)
{
var date = new Date(year, month, i);
if (date.getDay() == 6)
{
saturdays.push(date);
}
else if (date.getDay() == 0)
{
sundays.push(date);
}
};
console.log(sundays, saturdays);
var sat = new Date(input.getFullYear(), input.getMonth(), 6 - input.getDate() + getDay());
var sun = new Date(input.getFullYear(), input.getMonth(), getDay() + (input.getDate() - 6));
5条答案
按热度按时间4zcjmb1e1#
试试这个**FIDDLE**
字符串
在sat和sun
Array()
中,您可以获得特定月份的周六和周日e7arh2l62#
使用getDate方法很简单:
字符串
r1zhe5dt3#
字符串
yvfmudvl4#
根据你上面的描述,你可以使用-
字符串
将代码放入for循环,在月份变化前加7,在月份变化前减7。
JavaScript输入日期将为您处理月份和年份。
flseospp5#
字符串