Skip to content
Jim Posen edited this page Jun 19, 2013 · 1 revision

Make no assumptions about which timezone the user is in. In Ruby, DateTime objects are time zone aware in that dates with an offset are correctly parsed. Furthermore, Rails provides very nice ActiveSupport utilites for dealing with time zones in case you have to convert between time zones. Be careful with dates as well since a date in one time zone might be the next day in another.

We want to avoid rendering dates and times in HTML. They are, however, acceptable within article bodies. Instead, you should use the datetime_tag helper method. This renders an HTML <time> tag with the datetime attribute set and no text content. Javascript running on every page will render empty <time> tags to display the datetime in the user's locale.

The format of the datetime is a string passed as the second argument to the datetime_tag helper method. The string will be set as the data-format attribute on the tag. The symbols used to represent the datetime format is given by the date.format Javascript library. See the link for reference.