Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting timestamps

Tags:

How do you format Rails timestamps in a more human-readable format? If I simply print out created_at or updated_at in my view like this:

<% @created = scenario.created_at %> 

Then I will get:

2009-03-27 23:53:38 UTC

like image 530
alamodey Avatar asked Mar 29 '09 00:03

alamodey


People also ask

What is UTC timestamp format?

Times are expressed in UTC (Coordinated Universal Time), with a special UTC designator ("Z"). Times are expressed in local time, together with a time zone offset in hours and minutes. A time zone offset of "+hh:mm" indicates that the date/time uses a local time zone which is "hh" hours and "mm" minutes ahead of UTC.

What are T and Z in timestamp?

The T doesn't really stand for anything. It is just the separator that the ISO 8601 combined date-time format requires. You can read it as an abbreviation for Time. The Z stands for the Zero timezone, as it is offset by 0 from the Coordinated Universal Time (UTC).

What is timestamp format in SQL?

SQL Date Data Types TIMESTAMP - format: YYYY-MM-DD HH:MI:SS.


2 Answers

The strftime (from Ruby's Time) and to_formatted_s (from Rails' ActiveSupport) functions should be able to handle all of your time-formatting needs.

like image 87
Greg Campbell Avatar answered Oct 25 '22 03:10

Greg Campbell


Take a look at the I18n functionality. It allows you to do the following in your views:

<%= localize(scenario.created_at, :format => :long) %>

where the formats are defined in your locales. More info

like image 42
acw Avatar answered Oct 25 '22 02:10

acw