---
title: Custom Date And Time Formats In Rails
teaser: How to use i18n in Rails for custom time and date formats.
tags: web,rails
author: Dan Croak
published_on: 2010-02-16
---

We wanted to shorten "about 1 month ago" text in an information-rich table.

![wrapping onto multiple lines][wrapping]

[wrapping]: http://media.tumblr.com/tumblr_kxwfmbfcHS1qz5x9p.png

We can [override `distance_in_words`][diw-override] with the i18n <abbr
title="Application Programming Interface">API</abbr> (and, by extension,
`time_ago_in_words`).

[diw-override]: http://github.com/rails/rails/blob/4cbb9db0a5ff65b0a626a5b043331abefd89e717/actionpack/lib/action_view/helpers/date_helper.rb#L68-103

## Speaks English real good

A quick change to `config/locales/en.yml` and we're done:

```yaml
en:
  datetime:
    distance_in_words:
      less_than_x_seconds:
        other: '1 minute'
      half_a_minute: '1 minute'
      less_than_x_minutes:
        one: '1 minute'
      x_minutes:
        one: '1 minute'
        other: '{{count}} minutes'
      about_x_hours:
        one: '1 hour'
        other: '{{count}} hours'
      about_x_months:
        one: '1 month'
        other: '{{count}} months'
      about_x_years:
        one: '1 year'
        other: '{{count}} years'
      over_x_years:
        one: 'over 1 year'
        other: 'over {{count}} years'
```

The result:

![fits on one line](http://media.tumblr.com/tumblr_kxwfvxvbvB1qz5x9p.png)

You can see other options in
[svenfuchs/i18n](https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en-AU.yml).
