B
B
Banan442020-05-01 10:42:07
JavaScript
Banan44, 2020-05-01 10:42:07

How to quickly get an object with numbers and names of the days of the week given the month and year?

const month = 'January'
const year = 2020

// Какой-то код 

console.log(object)
/* 
{
  "Monday": [4, 5, 6 ,7]
  "Tuesday": [8, 9, 10, 11]
  ...
} */

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-05-01
@Banan44

function getDatesGroupedByWeekday(year, month) {
  const d = new Date(`${month} 1, ${year}`);
  const iMonth = d.getMonth();
  const result = {};

  while (d.getMonth() === iMonth) {
    const date = d.getDate();
    const weekday = d.toLocaleString('en-US', { weekday: 'long' });
    (result[weekday] ||= []).push(date);
    d.setDate(date + 1);
  }

  return result;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question