Sometimes we want to handle date as Date in JavaScript and JavaScript's JSON.parse converts Date to an string in UTC ISO 8601 format.
JSON.stringify({ date: new Date(2015, 2, 14) })
// "{"date":"2015-03-13T15:00:00.000Z"}"
ActiveRecord's date attribute doesn't take timezone into account when it converts string to date.
class Application < Rails::Application
config.time_zone = 'Tokyo'
end
class Person < ActiveRecord::Base
# birth_date :date
end
expect(Person.new(birth_date: '2015-02-13T15:00:00.000Z').birth_date)
.to eq(Date.new(2015, 2, 13))
StringDateAssignmentWithTimezone concern overrides date attribute mutators in your ActiveRecord model to take timezone into account.
class Person < ActiveRecord::Base
include StringDateAssignmentWithTimezone
end
expect(Person.new(birth_date: '2015-02-13T15:00:00.000Z').birth_date)
.to eq(Date.new(2015, 2, 14))