Created
September 30, 2022 07:29
-
-
Save ulybu/052c009893532141c4fbc0c2810c7e0e to your computer and use it in GitHub Desktop.
Creates a map that groups weeks of the year by month, like `[0: [1,2,3,4], 1:[5,6,7,8], 2: [9,10,11,12,13],,]`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const getWeekNumbersForYear = year => { | |
const date = dayjs(`${year}-01-01`) | |
const numberOfWeek = date.isoWeeksInYear() | |
const arrayOfWeekWithFirstDay = [] | |
for (let week = 1; week <= numberOfWeek; week += 1) { | |
const day = dayjs().year(year).isoWeek(week).day(1) | |
arrayOfWeekWithFirstDay.push({ | |
weekNumber: week, | |
startDay: day.format('YYYY-MM-DD') | |
}) | |
} | |
const result = new Array(12).fill(null).map(_ => []) | |
arrayOfWeekWithFirstDay.forEach((item, index) => { | |
const month = dayjs(item.startDay).month() | |
if (index === 0 && month === 12) { | |
result[0].push(item.weekNumber) | |
} else { | |
result[month].push(item.weekNumber) | |
} | |
}) | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment