[Ruby] How to Change the Display Format of Time Objects

Tadashi Shigeoka ·  Sun, November 13, 2011

I researched how to change the display format of Time objects in Ruby, so here’s a memo.

Time.now.strftime("%Y-%m-%d %H:%M:%S") #=> "2011-11-13 00:31:21"

For example, suppose you want to display a PostgreSQL timestamp type value like a date type value.

If the start_at record of the event table is a timestamp type, you can write it as follows:

@event.start_at.strftime("%Y-%m-%d") #=> "2011-11-13"

That’s all.

【References】

逆引きRuby - 日付と時刻

That’s all from the Gemba.