How to format ActiveSupport::TimeWithZone instance to specified format
You can use strftime
:
> date_time.strftime('%Y-%m-%d')
where,
%Y - Year with century
%m - Month of the year (01..12)
%d - Day of the month (01-31)
[7] pry(main)> Time.zone.now.is_a?(ActiveSupport::TimeWithZone)
=> true
[8] pry(main)> Time.zone.now.to_s
=> "2014-12-22 17:16:39 +1100"
[9] pry(main)> Time.zone.now.strftime("%Y-%m-%d")
=> "2014-12-22"
[11] pry(main)> Time.zone.now.strftime("%F")
=> "2014-12-22"
Refer
http://www.ruby-doc.org/core-2.1.5/Time.html#method-i-strftime