Imran Imran - 5 months ago 14
Ruby Question

Convert String to DateTime Ruby

I have a string

"2015-11-01T10:00:00.00+08:00"
extracted from json response. How can convert it to Time or DateTime?

I tried
Time.new("2015-11-01T10:00:00.00+08:00")
its returning
2015-01-01 00:00:00 +0530
, clearly the date is changed here and time too.

Answer
require 'date'

▶ Date.parse "2015-11-01T10:00:00.00+08:00"
#⇒ #<Date: 2015-11-01 ((2457328j,0s,0n),+0s,2299161j)>

▶ DateTime.parse "2015-11-01T10:00:00.00+08:00"
#⇒ #<DateTime: 2015-11-01T10:00:00+08:00 ((2457328j,7200s,0n),+28800s,2299161j)>