54
54

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Rails] date_select, datetime_selectを細かく指定。

Last updated at Posted at 2014-09-29

date_select, datetime_selectを細かく指定したい

<div class="birth_date_field">
  <%= f.label :birth_date %>
  <%= f.date_select(
          :birth_date,          
          use_month_numbers: true, # jan, febとかじゃなくて数字で月を表現
          start_year:     1930, # 何年から表示させるか
          end_year:       (Time.now.year), # 何年まで表示させるか
          default:        Date.new(1989, 1, 1), # 最初に表示されている年月日
          date_separator: '/') # 年月日を仕切る文字
   %>
</div>

もうちょっと日本ぽくすると。

<div class="birth_date_field">
  <%= f.label :birth_date %>
  <%= raw sprintf(
              f.date_select(
                  :birth_date,
                  use_month_numbers: true,
                  start_year:        1930,
                  end_year:          (Time.now.year - 18),
                  default:           Date.new(1989, 1, 1),
                  date_separator:    '%s'), # 年月日区切りをそれぞれ指定
              '年 ', '月 ') + '日'
   %>
</div>


参考

54
54
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
54
54

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?