Tutorials

Easy approach to format Dates in Rails projects

 

I saw a lot of projects where the default date format was used. In fact, customizing the date format to provide a human-readable date will be very welcomed from your customers!

So what should we do to start making our application awesome? No worries, it’s a very easy process that we will discover together.

Example

To get started we will need an example and to keep it simple we will use an administrative application that displays profiles and allows to export records as CSV.

The schema file:

And we have a class for exporting profiles in CSV format.

The first improvement is to use strftime  method from Ruby  DateTime class, for more details and possible options check this link for documentation.

We want to display birth dates with the following format 12-Jan-2012. So we update our private method  build_row  like so:

But wait, we should never use a hardcoded literal for formatting dates, we want to write easy to maintain applications 🙂

Our first option is to move the formatting to a constant inside the service class:

This is an improvement but let’s step back and ask this question:

  • if we need to use the same date format in other parts from our application, does it make sense to have a constant with this name:  GenerateReport::HUMAN_READABLE_DATE_FORMAT ?

So we need a better alternative. Usually, locales files are a good candidate for date formatting for two reasons:

  • Easy to edit and provide different formats based on locales.
  • Easy to format a date using I18n API.

Let’s see this in action:

First, we create a new date format inside config/locales/en.yml  with human_readable  as key.

Then we need just to pass the new key to the format option for I18n.localize.

and if we have an index view we can do the following:

The last example uses a Rails helper called l that delegates to I18n.localize (docs). It’s more common to see the alias used in Rails views but now you shouldn’t be surprised to see I18n.localize too.

Conclusion

We saw together how to format dates in Rails projects by using I18n API. For more details about this API or other uses cases, I invite you to visit this Rails guide.