Rails 7 adds :day_format option to date_select


There are very few inconsistencies with Rails, but the very few times it does, we’re left thinking: “How could they have missed this!”. One such inconsistency is with date_select. Though it has a myriad of options, it lacks in some basic ones.

Before

date_select provides users with an option to control the format of the year being displayed in the select box. The default select boxes look like this,

Using the year_format option allows us to configure the format in which the year gets printed out in the select box,

<%= f.date_select :birthday, year_format: -> (year) {"Year #{year}"} %>

This might seem trivial however, it happens that it is a crucial feature when developing in the Japanese language. In Japanese culture, the date 2021-11-29 is usually written like 2021年11月29日. 年 means year, 月 means month, and 日 means day.

However, Rails only provides the option for year_format and a workaround for configuring months (using the use_month_names option).

After

Rails 7 adds :day_format option to date_select method. Similar to year_format, we can now pass the day_format key to get a date in the desired format.

<%= f.date_select :published_date, day_format: -> (day) { day.ordinalize } %>

However, we feel that this feature is still incomplete without the month_format option.

Join Our Newsletter