0
0

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.

lメソッドについて

0
Posted at

目次

①lメソッドとは
②lメソッドの使い方

①lメソッドとは

日付や時刻をローカライズするメソッドのこと

②lメソッドの使い方

前提として以下のような記述があるとする。

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

lメソッドを使うことで、表示したい形で表示することができる。

 <%= l(day) %>

上記のように記述することで、ビューにデフォルトで"%Y/%m/%d %H:%M:%S"のように表示される。
日付だけ表示させたい時にはdefaultではなく、shotのような形式で表示したいとする
この場合以下のような記述する。
formatオプションを付けると、書式を切り替えることができる。

 %= l(day, format: :short) %>
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?