Display formatted date

Hi,
In my form I have date field set as ‘datetime_select’ which is fine as I
wanted it in the same format. But while displaying (list action) it
displays date in long format i.e. ‘Tue May 30 15:39:00 Central Daylight
Time 2006’. How can I format it so that it will just be diaplyed as
‘dd/mm/yyyy’ format?

Thanks

Look at Time#strftime:

http://corelib.rubyonrails.org/

<%= object.created_at.strftime("%d/%m/%Y") %>

On 6/14/06, Ruby R. [email protected] wrote:

Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Derek H.
HighGroove Studios - http://www.highgroove.com
San Mateo, CA | Atlanta, GA
Keeping it Simple.
650.276.0908

Alterantively to using strftime every time you want to format a date, ie
if
you want this display application wide you can add

ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.update(
:default => '%d/%m/%Y @ %H:%M: %p )

ActiveSupport::CoreExtensions::date::Conversions::DATE_FORMATS.update(
:default => '%d/%m/%Y )

to the environment.rb

Then you can just put something like
<%= object.created_at.to_date %>

and all the formatting is done for you.

Thanks guys I wasn’t aware of this!

On 13 Jul 2006 03:49:36 -0000, Kevin O. <
[email protected]> wrote:

ActiveSupport::CoreExtensions::date::Conversions::DATE_FORMATS.update(

In my form I have date field set as ‘datetime_select’ which is
Posted via http://www.ruby-forum.com/.
San Mateo, CA | Atlanta, GA
Rails mailing list
formatting in the view.

_Kevin

Kevin, Thanx for the heads up…

On Thursday, June 15, 2006, at 2:53 PM, Daniel N wrote:

Time 2006’. How can I format it so that it will just be diaplyed as

[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

I just came across an interesting bug that crops up when you do this.
On MySQL 5.0.21 if I use this trick, it prevents ‘Date’ fields from
updating properly. The same may happen with other types, but I haven’t
tested them.

It is probably safer to leave these settings alone and just force the
formatting in the view.

_Kevin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs