Search code examples
javascriptreactjscalendardayjs

How to detect first day of a month with Day.js


I am building a calendar component in React without using any library but Day.js.

I need to detect the first weekday of the month in order to leave a gap as shown in the picture. Otherwise the first day of every month has to be Sunday. How can I detect the first weekday of the given date?

calendar


Solution

  • Day.js has built in function to achieve this

    dayjs().startOf("month").day() 
    

    This method returns current month's first week day as number.

    0 = Sunday, 6 = Saturday