最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to get Saturdays and Sundays (dates) of a month + JAVASCRIPT - Stack Overflow

programmeradmin2浏览0评论

If a date is given (for example-"2014-01-07") ,I want to access that specific month (January 2014) and get the date number of the Saturdays and Sundays of that month.

In this case, 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

If a date is given (for example-"2014-01-07") ,I want to access that specific month (January 2014) and get the date number of the Saturdays and Sundays of that month.

In this case, 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

Share Improve this question asked Jan 3, 2014 at 7:23 Amila IddamalgodaAmila Iddamalgoda 4,28612 gold badges50 silver badges86 bronze badges 0
Add a ment  | 

5 Answers 5

Reset to default 6

Try with this 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 sat and sun Array() you can get the Saturdays and Sundays of particular month

It is very easy using getDate method:

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

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.

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

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

发布评论

评论列表(0)

  1. 暂无评论