Display client-side local time with gem local_time

Tram Ho


Gem local_time makes it easy for us to display the user’s date time with their local time. It supports the built-in rails helper to render into <time> tags in UT, then its js will convert that tag always from UTC to local browser time.


  • Add gem "local_time" to Gemfile.

    => bundle install

  • Add local-time.js in application.js

Usage and examples

This gem already has the following helper available:

Time and date helpers

  • Default

For example

  • Format with strftime

Display datetime (default format)

Display date (default format)

Use the strftime format defined in the App:

In this format, it will scan in my App in the following order:

I18n.t("time.formats.#{format}") , I18n.t("date.formats.#{format}") , Time::DATE_FORMATS[format] and Date::DATE_FORMATS[format]

Supported strftime format:

%a %A %b %B %c %d %e %H %I %l %m %M %p %P %S %w %y %Y %Z

Time ago helpers

Displays the amount of time elapsed compared to the current time

For example with English:

  • Recent: “a second ago”, “32 seconds ago”, “an hour ago”, “14 hours ago”
  • Yesterday: “yesterday at 5:22 pm”
  • This week: “Tuesday at 12:48 am”
  • This year: “on Nov 17”
  • Last year: “on Jan 31, 2012”


Internationalization (I18n)

Its default will be English, but you can also create different languages. You can create the following new locale file with the format as its default file en translations :

You can find details in the official document here:


Share the news now

Source : Viblo