Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { isSameMonth, startOfMonth } from 'date-fns'
- export const generateCalendar = (
- firstDateOfMonth: Date
- ): number[][] => {
- const date = startOfMonth(firstDateOfMonth)
- const getDay = (date: Date) => {
- let day = date.getDay()
- if (day === 0) day = 7
- return day - 1
- }
- const calendar: number[][] = [[]]
- for (let i = 0; i < getDay(date); i++) {
- calendar[calendar.length - 1].push(0)
- }
- while (isSameMonth(date, firstDateOfMonth)) {
- calendar[calendar.length - 1].push(date.getDate())
- if (getDay(date) % 7 == 6) {
- calendar.push([])
- }
- date.setDate(date.getDate() + 1)
- }
- if (getDay(date) != 0) {
- for (let i = getDay(date); i < 7; i++) {
- calendar[calendar.length - 1].push(0)
- }
- }
- return calendar
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement