Search code examples
rubyjruby

DateTime.iso8601('2014-07-08T17:51:36.000Z') results in NoMethodError?


I can't seem to get DateTime to handle a legitimate ISO-8601 String.

jruby-1.7.19 :013 > DateTime.iso8601('2014-07-08T17:51:36.000Z')
NoMethodError: undefined method `/' for "000":String
        from /Users/robert.kuhar/.rvm/rubies/jruby-1.7.19/lib/ruby/1.9/date.rb:1778:in `new_by_frags'
        from /Users/robert.kuhar/.rvm/rubies/jruby-1.7.19/lib/ruby/1.9/date.rb:1829:in `iso8601'
        from (irb):13:in `evaluate'
        from org/jruby/RubyKernel.java:1107:in `eval'
        from org/jruby/RubyKernel.java:1507:in `loop'
        from org/jruby/RubyKernel.java:1270:in `catch'
        from org/jruby/RubyKernel.java:1270:in `catch'
        from /Users/robert.kuhar/.rvm/rubies/jruby-1.7.19/bin/irb:13:in `(root)'

How do I dig out of this?


Solution

  • I don't know why DateTime.iso8601 breaks under JRuby, but you can use DateTime.parse instead:

    d = DateTime.parse '2014-07-08T17:51:36.000Z'
    puts d.to_s #=> "2014-07-08T17:51:36+00:00"