How to get all days in a month and sign the week number?

  • 0
    Here's an example:
    [
    	{
    		day: 1,
    		weekday: "Fri",
    		weeknumber: 1
    	},
    	{
    		day: 2,
    		weekday: "Sat",
    		weeknumber: false,
    	}
    	......
    	{
    		day: 4,
    		weekday: "Mon",
    		weeknumber: 2
    	},
    	{
    		day: 5,
    		weekday: "Tue",
    		weeknumber: false
    	}
    ]
    JavaScript Anastasia Robbins, Oct 21, 2019

  • 2 Answers
  • 0
    function getDays(year, month) {
    const days = [];
    const d = new Date(year, month, 1);
    let week = 1;

    while (d.getMonth() === month) {
    const date = d.getDate();
    const day = d.getDay();

    days.push({
    day: date,
    weeknumber: (day === 1 || date === 1) ? week : false,
    weekday: d.toLocaleString('en-US', { weekday: 'short' }),
    });

    d.setDate(date + 1);
    week += !day;
    }

    return days;
    }
    Anonymous

  • 0
    Current week number

    (+new Date() - +new Date(2020, 1, 1)) / 1000 / 60 / 60 / 24 / 7


    What does it mean - "get all the days of the month"?

    If you need to get an array of the type that you gave in the example, then get the number of days in a month and forward into a loop.

    Also here for you https://developer.mozilla.org/en/ docs / Web / JavaScri ...
    Connor Ponce

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!