# Overriding Date#to\_s

**URL:** <https://www.ruby-forum.com/t/overriding-date-to-s/182024>\
**Category:** Rails\
**Created:** [January 21, 2010, 6:34pm UTC](https://www.ruby-forum.com/t/overriding-date-to-s/182024 "2010-01-21T18:34:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![marnen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/67e7ee/32.png) [@marnen](https://www.ruby-forum.com/u/marnen)\
**Post date:** [January 21, 2010, 6:34pm UTC](https://www.ruby-forum.com/t/overriding-date-to-s/182024/1 "2010-01-21T18:34:31Z")

</div>

Hi everyone.

I just ran into an issue which I solved in an…er…interesting way,  
and I’m trying to figure out if my solution is brilliant or terrible.

Basically, I have a Date attribute in one of my AR models which I would  
like my views to display with a particular format (other than what  
Date#to\_s gives). Rather than have to call a formatting method on this  
attribute every time I use it in a view, what I have done is to override  
the to\_s method on the object’s singleton class.

Does this make sense? Is there a better way to handle date formatting  
in Rails?

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![marnen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/67e7ee/32.png) [@marnen](https://www.ruby-forum.com/u/marnen)\
**Post date:** [January 21, 2010, 6:59pm UTC](https://www.ruby-forum.com/t/overriding-date-to-s/182024/2 "2010-01-21T18:59:59Z")

</div>

On Thu, Jan 21, 2010 at 7:34 PM, Marnen Laibow-Koser  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Does this make sense? Â Is there a better way to handle date formatting  
> in Rails?

There is a way to specify different date formats in rails, checkout  
the “Adding your own time formats to to\_formatted\_s” section of the  
API.

[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Date/Conversions.html](http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Date/Conversions.html)

---

<div class="post-metadata">

**Author:** ![marnen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/67e7ee/32.png) [@marnen](https://www.ruby-forum.com/u/marnen)\
**Post date:** [January 21, 2010, 8:08pm UTC](https://www.ruby-forum.com/t/overriding-date-to-s/182024/3 "2010-01-21T20:08:56Z")

</div>

Christos Trochalakis wrote:

> On Thu, Jan 21, 2010 at 7:34 PM, Marnen Laibow-Koser  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Does this make sense? Â Is there a better way to handle date formatting  
> > in Rails?
> 
> There is a way to specify different date formats in rails, checkout  
> the “Adding your own time formats to to\_formatted\_s” section of the  
> API.
> 
> [http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Date/Conversions.html](http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Date/Conversions.html)

Thanks! I _thought_ Rails had something like this, but I was looking in  
the wrong place. Duhhh.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
