如何获得一个月的周六和周日(日期)+ JAVASCRIPT

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20898661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:34:46  来源:igfitidea点击:

How to get Saturdays and Sundays (dates) of a month + JAVASCRIPT

javascriptjquery

提问by Amila Iddamalgoda

If a date is given (for example-"2014-01-07") ,I want to access that specific month (January 2014) and get the date numberof the Saturdaysand Sundaysof that month.

凡是注日期定(为示例- “2014年1月7日”),我要访问特定月(2014年1月),并获得日数的的周六周日是一个月。

In this case, 4,11,18,25 --Saturdays 5,12,19,26 -- Sundays

在这种情况下,4,11,18,25 --Saturdays 5,12,19,26 -- Sundays

I want to use java script because this is a front end development. Help me out with this..Thnkz

我想使用 java 脚本,因为这是一个前端开发。帮我解决这个问题..Thnkz

回答by yashhy

Try with this FIDDLE

试试这个FIDDLE

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();
}

In satand sunArray()you can get the Saturdays and Sundays of particular month

satsun 中,Array()您可以获得特定月份的周六和周日

回答by Dmitriy.Net

It is very easy using getDatemethod:

使用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);

回答by Kapil Dave

As per your discription above you can use -

根据您上面的描述,您可以使用 -

var sat = new Date(input.getFullYear(), input.getMonth(), 6 - input.getDate() + getDay());
var sun = new Date(input.getFullYear(), input.getMonth(), getDay() + (input.getDate() - 6));

Put that code in for loop and add 7 till month changes. and subtract 7 till month changes.

将该代码放入 for 循环并添加 7 直到月份更改。并减去 7 直到月份变化。

javascript input date will takecare of month and year for you.

javascript 输入日期将为您处理月份和年份。

回答by YOUNG SANG

var d = new Date();
var pred = new Date(d.getFullYear(),d.getMonth()+1,0).getDate();

var nowd;
var sat_array = "";
var sun_array = "";


for (i=1;i<=pred;i++) {

  try {
    console.log(d.getFullYear()+ "-" + (d.getMonth()+1) + "-" + i);
    nowd = new Date(d.getFullYear()+ "-" + (d.getMonth()+1) + "-" + i)

    if (nowd.getUTCDay() == 5)
    {
        sat_array = sat_array + "," + i;
    }

    if (nowd.getUTCDay() == 6)
    {
        sun_array = sun_array + "," + i;
    }


  }
  catch(e) {
      return;
  }

}

console.log("SAT list : " +sat_array);
console.log("SUN list : " +sun_array);