---
title: Custom Formats for DateTime
teaser:
tags: web,rails
author: Gabe Berke-Williams
published_on: 2011-08-16
---

`Date::DATE_FORMATS` is quite helpful. It lets you do this:

```ruby
Date.today.to_s(:custom)
```

with only this code in `config/initializers/date_format.rb`:

    Date::DATE_FORMATS[:custom] = "%Y-%m-%d"

To do the same thing for `DateTime` instances, like `created_at` columns, use
[`Time::DATE_FORMATS`](http://api.rubyonrails.org/v3.0.8/classes/Time.html).

```ruby
Time::DATE_FORMATS
```

To set the default format for either one, set `DATE_FORMATS[:default]`. This
will cause `<%= item.created_at %>` to output `"2011-08-16"`, with no extra work
from you.
