jquery 如何获得一个月的星期六和星期日(日期)+ JavaScript

6g8kf2rb  于 2023-11-17  发布在  jQuery
关注(0)|答案(5)|浏览(127)

如果给定了日期(例如-“2014-01-07”),我希望访问该特定月份(2014年1月)并获取该月份星期六星期日日期编号
在这个例子中,4,11,18,25--星期六5,12,19,26--星期天
我想使用java脚本,因为这是一个前端开发.帮助我与此.. Thnkz

4zcjmb1e

4zcjmb1e1#

试试这个**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();
}

字符串
satsunArray()中,您可以获得特定月份的周六和周日

e7arh2l6

e7arh2l62#

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

字符串

r1zhe5dt

r1zhe5dt3#

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

字符串

yvfmudvl

yvfmudvl4#

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

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

字符串
将代码放入for循环,在月份变化前加7,在月份变化前减7。
JavaScript输入日期将为您处理月份和年份。

flseospp

flseospp5#

function getWend(date){
    var dArray = date.split('-')
    for(i=0;i<=31;i++){
        var nDay = dArray[0]+'-'+dArray[1]+'-'+i.toString()
        if([6,0].includes(new Date(nDay).getDay()))
        console.log(new Date(nDay).toString())
    }
}

getWend('2023-10-27')

字符串

相关问题