Lesha Pipiev
Lesha Pipiev

Reputation: 3333

Rails 3 Activerecord TimeWithZone to String without offset

I have:

irb(main):016:0> t[0].change_date
=> Tue, 08 May 2012 18:33:53 FET +03:00
irb(main):017:0> t[0].change_date.class
=> ActiveSupport::TimeWithZone
irb(main):018:0> t[0].change_date.to_s
=> "2012-05-08 18:33:53 +0300"

How Can I get a string representation of datetime but without offset +3000? I need only "2012-05-08 18:33:53"

Upvotes: 0

Views: 517

Answers (2)

naren
naren

Reputation: 937

You can use the helper method in ActiveSupport like

 t[0].change_date.to_formatted_s(:db)

As the to_fomatted_s is aliased to_s you can also use

 t[0].change_date.to_s(:db)

You can check support for time formats in the documentation date format (http://api.rubyonrails.org/classes/Date.html#DATE_FORMATS)

Upvotes: 1

Aayush Kumar
Aayush Kumar

Reputation: 1618

You can use strftime to format the way you would like to view the datetime. The strftime documentation.

So, in your case, if you would like to see the date as: 2012-05-08 18:33:53, then use:

t[0].change_date.strftime("%Y-%m-%d %H:%M:%S")

Upvotes: 1

Related Questions