[removed] get array of day names of given date (month/year)

前端 未结 6 1803
我寻月下人不归
我寻月下人不归 2021-01-19 02:59

how can i get the whole month in day namesof given month/year? like :

var year = \"2000\";
var month = \"7\"

... some code here that makes an array with nam         


        
6条回答
  •  醉梦人生
    2021-01-19 03:07

    I know this is an old question, but today a question linked to this, and I'd like to point out one don't really need to do much things other than convert day (0-6) to string (Sunday-Saturday)

    function getDays(year, month) { //month: 1-based, as normal person
        --month //change it to 0-based, as Date object
        let dayname = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
        let date = new Date(year, month)
        let result = []
        while(date.getMonth()==month){
          result.push(`${date.getDate()}. ${dayname[date.getDay()]}`)
          date.setDate(date.getDate()+1)
        }
        return result;
    }
    
    console.log(getDays(2019,12))

提交回复
热议问题