我有一个我认为是ISO8601格式的字符串,例如2010-02-11T14:54:27.000Z
。如何将此字符串转换为datetime
类型,以便保存?
我认为rails还要求您在保存日期时保存时区。
我尝试过使用Date.parse(2010-02-11T14:54:27.000Z),但它没有用
答案 0 :(得分:0)
require 'date'
# If you want the date & time
DateTime.parse('2010-02-11T14:54:27.000Z')
# If you only care about the date
Date.parse('2010-02-11T14:54:27.000Z')
2.1.1p76下的测试
~|⇒ ruby -v
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0]
~|⇒ irb
2.1.1 :001 > require 'date'
=> true
2.1.1 :002 > dt = DateTime.parse('2010-02-11T14:54:27.000Z')
=> #<DateTime: 2010-02-11T14:54:27+00:00 ((2455239j,53667s,0n),+0s,2299161j)>
2.1.1 :003 > puts dt.to_s
2010-02-11T14:54:27+00:00