LoginSignup
0
0

More than 1 year has passed since last update.

【Ruby on Rails】 日時データを年月日の形式で表示する

Last updated at Posted at 2022-01-11

日時データを年月日で表示したい

scheduleというテーブルの中から、startという日時データのカラムの値を表示したい時、

<%= schedule.start %>

↑こんな感じで書くと

スクリーンショット 2022-01-11 21.34.44.png
↑こんな感じで表示される。
これを、年月日だけで表示していきたい。

対処法

<%= schedule.start&.strftime("%Y年 %m月 %d日") %>

↑このように書くと、
スクリーンショット 2022-01-11 21.56.53.png
↑こんな感じで表示される。

時、分まで表示する

更新日時(updated_at)など、何時何分の部分を表示したい時は、

<%= schedule.updated_at&.strftime("%Y年 %m月 %d日 %H時 %M分") %>

こんな感じの書き方になる。

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