Matt Matt - 6 months ago 26
Ruby Question

ActiveRecord mapping date to TimeWithZone instead of DateTime with .000000 suffix

Like in the title, the program map DateTime to TimeWithZone itself.

create_table :db_values do |t|
t.datetime :created_date

record from database:

How can I remove that suffix? '.000000'
I used :

Time::DATE_FORMATS[:db]= lambda { |time| time.strftime("%Y-%m-%dT%H:%M:%S%z") }


Rails will save all times/timestamps in UTC timezone into your DB. This way it is easier to know exactly to what time it should compare it later when you give it a time in some other timezone. So, don't try to change the DB values to zoned time values. Go with the flow...

When it comes to displaying localized times, just use helper method I18n.l Time.current.

Here are a couple of good articles about timezones in Rails.