Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question