LoginSignup
10
8

More than 5 years have passed since last update.

Time型のデータの値を、時刻部分だけ表示する

Posted at

問題:Time型のデータの表示に余計なものがくっついてくる

Time型のデータをRailsのビューで表示する時、そのままで呼び出してしまうと余計な年月日まで表示されてしまう。

↓2000-01-01 07:00(←ここだけ表示したい):00 UTC となっている。
image.png

それを、↓のように時刻だけにしたい。
image.png

なお、データベースには以下のようなデータが保存されている。
image.png

解決方法

strftimeを用いて日付をフォーマットする。

# もともとのコード(抜粋)
"#{t.departure_time}発"

# 解決後のコード(抜粋)
"#{t.departure_time.strftime('%H:%M')}発"

%Hなどは表示したいものに合わせて変える。

参照:
http://railsdoc.com/references/strftime

10
8
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
10
8