Back

Month calendar view helpers in Ruby on Rails

I’m learning how to build an interactive calendar UI with Rails and Hotwire. Take a look at this nicely designed calendar from Tailwind UI.

Tailwind UI month calendar

I wondered how I would generate a full month’s dates but also include the last week of the previous month and the first week of the following month enough to fill in a 7x5 or 7x6 grid. Luckily, Rails has some handy date helpers that make this easy, and they read like I’m writing a sentence.

date.beginning_of_month.beginning_of_week..date.end_of_month.end_of_week

I love how that one line will generate the exact date range I need. Take a look at the docs for DateAndTime::Calculations to learn more about the beginning_of and end_of helpers.