Formatting Dates in my view

Hi,

Currently my dates look like this: 2009-10-10 00:55:37 UTC

I want to format them in my view to look like this: 10 Oct 2009 00:55

Is it possible to do the formatting in the view using the to_s method?

Thanks,

David

David S. wrote:

Currently my dates look like this: 2009-10-10 00:55:37 UTC
I want to format them in my view to look like this: 10 Oct 2009 00:55
Is it possible to do the formatting in the view using the to_s method?

How about:
http://apidock.com/rails/ActiveSupport/TimeWithZone/to_s

Aldric G. wrote:

David S. wrote:

Currently my dates look like this: 2009-10-10 00:55:37 UTC
I want to format them in my view to look like this: 10 Oct 2009 00:55
Is it possible to do the formatting in the view using the to_s method?

How about:
http://apidock.com/rails/ActiveSupport/TimeWithZone/to_s

Thanks for the quick response. This is exactly what I was looking for!

2009/10/12 David S. [email protected]:

Thanks for the quick response. This is exactly what I was looking for!

You can use strftime if you need more control.

Colin

Time.now.strftime("%d %b %Y %H:%M") => “12 Oct 2009 18:50”

On Oct 12, 2:29 pm, David S. [email protected]

This is how I format dates in one of my projects:

message.created_at.strftime("%b %d %Y at %H:%M")

That gives “Jun 12 2009 at 11:34” if I remember correctly. Just play
with it, very simple.

http://www.ruby-doc.org/core/classes/Time.html#M000298

Cheers.

You can put something like this in your environment.rb if you will be
using that format often:

ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
:show_time => “%l:%M%p”,
:reminder_time => “%B %d, %Y”,
:simple_time => “%H:%M:%S”
)

Then just to something like Time.now.to_s(:simple_time)

Don’t forget to restart your server.

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