Ruby and Ruby on Rails come with a bunch of time saving libraries, which can save you a lot of time coding for specific scenarios.
These are the methods which ship with Ruby’s DateTime class, with their associated strftime format. Where possible you should use these when storing times, or outputting data in some machine readable way (Like an API).
| || ||Sun, 03 Mar 2019 13:45:03 GMT|
| || ||2019-03-03T13:45:17+00:00|
| || ||H31.03.03T13:45:26+00:00|
| || ||2019-03-03T13:45:37+00:00|
| || ||Sun, 3 Mar 2019 13:45:48 +0000|
| || ||Sun, 3 Mar 2019 13:45:58 +0000|
| || ||2019-03-03T13:49:32+00:00|
| || ||1551621698|
Non-developers are a bit rubbish at reading those standardised formats. Luckily, Rails has your back with a bit of I18n awesomeness! It ships with 3 localisation formats (default, short & long), that’ll output dates nicely.
| ||Sun, 03 Mar 2019 14:17:01 +0000|
| ||03 Mar 14:17|
| ||March 03, 2019 14:17|
By default, rails ships with the
:en locale. However, pretty often you’ll want to localise your dates and times to different international formats, for this the rails-i18n gem to save you a bunch of time.
Add that gem to your Gemfile, then you can either set the locale from the
Accept-Language browser header, or just pass the
format argument to your
I18n.l method call.
| ||March 03, 2019 02:37 PM|
| ||03 March, 2019 14:38|
| ||Sonntag, 03. März 2019, 14:38 Uhr|
This should handle most your date & time formatting needs when outputting to various users :D