LoginSignup
3
2

More than 1 year has passed since last update.

【Rails】I18n#lで日付のフォーマットを定義しよう

Posted at

環境

Rails 6.0.1
Ruby 2.6.3
PostgreSQL 11.16

日付のフォーマットを定義

config/locales/ja.yml
ja:
  date:
    formats:
      default: "%Y.%m.%d"
      long: "%Y年%m月%d日"
      short: "%m月%d日"
  time:
    formats:
      default: "%Y/%m/%d %H:%M:%S"
      long: "%Y年%m月%d日(%a) %H時%M分%S秒 %z"
      short: "%y/%m/%d %H:%M"
      date_long: "%Y年%m月%d日"
      date_short: "%m月%d日"
      datetime: "%Y.%m.%d %H時%M分"
      only_time: "%H:%M"

デフォルトの形は引数の指定なし、それ以外は第2引数を指定するとデフォルト以外のフォーマットに変換できる。

I18n.l(Date.today)
#=> "2022.01.01"
I18n.l(Date.today, format: :long)
#=> "2022年01月01日"

参考

3
2
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
3
2