Find all the days in a month with Date object?

后端 未结 6 1010
Happy的楠姐
Happy的楠姐 2020-11-30 01:07

is there a way to have all the days of a month or of a year? I am looking for this in order to disable some specific days in a datepicker, i would have a page in the back-of

6条回答
  •  被撕碎了的回忆
    2020-11-30 01:47

    I'm not sure from your description if the standard disable dates datepicker will work with you, so I'll answer you question directly.

    You can construct an array of days for a month fairly easily by doing this:

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

    ​ Using that array of days you can pretty much do whatever you want with it and know the day of the week as well.

提交回复
热议问题