Last Updated: February 25, 2016
·
1.093K
· rorcraft

DateTime iso8601

DateTime.iso8601 or to_json does not return UTC with "Z"

ActiveSupport::TimeWithZone has some interesting behaviors.

require 'active_support/core_ext'
 => true

ActiveSupport.use_standard_json_time_format
 => true

DateTime.new(2013, 5, 28).to_json
 => ""2013-05-28T00:00:00+00:00""

DateTime.new(2013, 5, 28).in_time_zone('UTC').to_json
 => "\"2013-05-28T00:00:00Z\""

ActiveSupport.use_standard_json_time_format = false
 => false

DateTime.new(2013, 5, 28).in_time_zone('UTC').to_json
 => "\"2013/05/28 00:00:00 +0000\""

DateTime.new(2013, 5, 28).in_time_zone('UTC').iso8601
 => "2013-05-28T00:00:00Z"     

DateTime.new(2013, 5, 28).iso8601
 => "2013-05-28T00:00:00+00:00"

DateTime.new(2013, 5, 28).to_json
 => "\"2013/05/28 00:00:00 +0000\""