Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. import { isSameMonth, startOfMonth } from 'date-fns'
  2.  
  3. export const generateCalendar = (
  4. firstDateOfMonth: Date
  5. ): number[][] => {
  6. const date = startOfMonth(firstDateOfMonth)
  7.  
  8. const getDay = (date: Date) => {
  9. let day = date.getDay()
  10. if (day === 0) day = 7
  11. return day - 1
  12. }
  13.  
  14. const calendar: number[][] = [[]]
  15. for (let i = 0; i < getDay(date); i++) {
  16. calendar[calendar.length - 1].push(0)
  17. }
  18.  
  19. while (isSameMonth(date, firstDateOfMonth)) {
  20. calendar[calendar.length - 1].push(date.getDate())
  21.  
  22. if (getDay(date) % 7 == 6) {
  23. calendar.push([])
  24. }
  25.  
  26. date.setDate(date.getDate() + 1)
  27. }
  28.  
  29. if (getDay(date) != 0) {
  30. for (let i = getDay(date); i < 7; i++) {
  31. calendar[calendar.length - 1].push(0)
  32. }
  33. }
  34.  
  35. return calendar
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement