# Custom date format

**URL:** <https://www.ruby-forum.com/t/custom-date-format/55765>\
**Category:** Rails\
**Created:** [March 23, 2006, 6:10pm UTC](https://www.ruby-forum.com/t/custom-date-format/55765 "2006-03-23T18:10:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chriscole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5fc32e/32.png) [@chriscole](https://www.ruby-forum.com/u/chriscole)\
**Post date:** [March 23, 2006, 6:10pm UTC](https://www.ruby-forum.com/t/custom-date-format/55765/1 "2006-03-23T18:10:20Z")

</div>

Hi, I’ve followed these directions :  
[http://railswiki.pdxruby.org/HowToDefineYourOwnDateFormat.html](http://railswiki.pdxruby.org/HowToDefineYourOwnDateFormat.html)

It seems pretty straightforward but obviously it doesn’t work for me…  
article.date\_edited.to\_formatted\_s(:my\_format\_1)

produce a default formatted date, not the one I’ve defined in  
environment.rb :  
ActiveSupport::CoreExtensions:📅:Conversions::DATE\_FORMATS.merge!(  
:my\_format\_1 =\> ‘%d/%m/%Y - %H:%M’,  
:my\_format\_2 =\> ‘%l:%M %p, %B %d, %Y’  
)

What’s wrong ?

Thanks

---

<div class="post-metadata">

**Author:** ![chriscole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5fc32e/32.png) [@chriscole](https://www.ruby-forum.com/u/chriscole)\
**Post date:** [March 23, 2006, 8:56pm UTC](https://www.ruby-forum.com/t/custom-date-format/55765/2 "2006-03-23T20:56:57Z")

</div>

Christophe G. wrote:

> Hi, I’ve followed these directions :  
> [http://railswiki.pdxruby.org/HowToDefineYourOwnDateFormat.html](http://railswiki.pdxruby.org/HowToDefineYourOwnDateFormat.html)
> 
> It seems pretty straightforward but obviously it doesn’t work for me…  
> article.date\_edited.to\_formatted\_s(:my\_format\_1)
> 
> produce a default formatted date, not the one I’ve defined in environment.rb :  
> ActiveSupport::CoreExtensions:📅:Conversions::DATE\_FORMATS.merge!(  
> :my\_format\_1 =\> ‘%d/%m/%Y - %H:%M’,  
> :my\_format\_2 =\> ‘%l:%M %p, %B %d, %Y’  
> )

What does “it doesn’t work for me” mean?

I’m doing this and it works fine. Just make sure the format definitions  
in your environment.rb go outside the run block.

By the way, you can just use some\_date.to\_s(:format) - Date#to\_s is  
aliased to Date#to\_formatted\_s

–josh  
http:blog.hasmanythrough.com

---

<div class="post-metadata">

**Author:** ![chriscole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5fc32e/32.png) [@chriscole](https://www.ruby-forum.com/u/chriscole)\
**Post date:** [March 23, 2006, 9:11pm UTC](https://www.ruby-forum.com/t/custom-date-format/55765/3 "2006-03-23T21:11:05Z")

</div>

Josh S. wrote:

> Christophe G. wrote:
> 
> > Hi, I’ve followed these directions :  
> > [http://railswiki.pdxruby.org/HowToDefineYourOwnDateFormat.html](http://railswiki.pdxruby.org/HowToDefineYourOwnDateFormat.html)
> > 
> > It seems pretty straightforward but obviously it doesn’t work for me…  
> > article.date\_edited.to\_formatted\_s(:my\_format\_1)
> > 
> > produce a default formatted date, not the one I’ve defined in environment.rb :  
> > ActiveSupport::CoreExtensions:📅:Conversions::DATE\_FORMATS.merge!(  
> > :my\_format\_1 =\> ‘%d/%m/%Y - %H:%M’,  
> > :my\_format\_2 =\> ‘%l:%M %p, %B %d, %Y’  
> > )
> 
> What does “it doesn’t work for me” mean?

It means that my\_format\_1 has no effect at all when used with  
to\_formatted\_s, Rails displays the default date format instead.

> I’m doing this and it works fine. Just make sure the format definitions  
> in your environment.rb go outside the run block.

It is

> By the way, you can just use some\_date.to\_s(:format) - Date#to\_s is  
> aliased to Date#to\_formatted\_s

Thank for the hint Josh,

I’m working on a Rails Engine and I wonder how it could be related with  
the problem ? I really don’t understand…

Just to be sure, here is the content of the files again :

environment.rb :  
ActiveSupport::CoreExtensions:📅:Conversions::DATE\_FORMATS.merge!(  
:my\_format\_1 =\> ‘%d/%m/%Y - %H:%M’,  
:my\_format\_2 =\> ‘%l:%M %p, %B %d, %Y’  
)

the view - list.rhtml  
\<%= article.date\_edited.to\_s(:my\_format\_1) %\>

---

<div class="post-metadata">

**Author:** ![chriscole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5fc32e/32.png) [@chriscole](https://www.ruby-forum.com/u/chriscole)\
**Post date:** [March 23, 2006, 11:58pm UTC](https://www.ruby-forum.com/t/custom-date-format/55765/4 "2006-03-23T23:58:12Z")

</div>

I’ve tried with the console and everything is ok :  
my\_format\_1 is listed and Date.today.to\_s(:my\_format\_1) returns a date  
formatted accordingly to the format…

The record in the db does contain a date value and trying different  
formats (eg. :short) works fine in the view

I really don’t understand…

But at least I’m learning about the console ! Great tool !

---

<div class="post-metadata">

**Author:** ![chriscole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5fc32e/32.png) [@chriscole](https://www.ruby-forum.com/u/chriscole)\
**Post date:** [March 23, 2006, 11:03pm UTC](https://www.ruby-forum.com/t/custom-date-format/55765/5 "2006-03-23T23:03:02Z")

</div>

Christophe G. wrote:

> Just to be sure, here is the content of the files again :
> 
> environment.rb :  
> ActiveSupport::CoreExtensions:📅:Conversions::DATE\_FORMATS.merge!(  
> :my\_format\_1 =\> ‘%d/%m/%Y - %H:%M’,  
> :my\_format\_2 =\> ‘%l:%M %p, %B %d, %Y’  
> )
> 
> the view - list.rhtml  
> \<%= article.date\_edited.to\_s(:my\_format\_1) %\>

That looks to me like it should work. I suggest checking things out in  
the console. Go in script/console and examine  
ActiveSupport::CoreExtensions:📅:Conversions::DATE\_FORMATS to make  
sure your format additions worked, then try printing  
Date.today.to\_s(:my\_format\_1). Also, look in the database and make sure  
there is a date in the field, try printing it with a different format,  
etc.

–josh  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![chriscole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5fc32e/32.png) [@chriscole](https://www.ruby-forum.com/u/chriscole)\
**Post date:** [March 24, 2006, 12:03am UTC](https://www.ruby-forum.com/t/custom-date-format/55765/6 "2006-03-24T00:03:37Z")

</div>

Christophe G. wrote:

> I’ve tried with the console and everything is ok :  
> my\_format\_1 is listed and Date.today.to\_s(:my\_format\_1) returns a date  
> formatted accordingly to the format…
> 
> The record in the db does contain a date value and trying different  
> formats (eg. :short) works fine in the view
> 
> I really don’t understand…
> 
> But at least I’m learning about the console ! Great tool !

Last thing I can think of is to quit and restart your server. I think  
you have to do that to get changes in the environment.rb to load.

–josh  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
