Ruby - 将字符串转换为日期

Say*_*yuj 11 ruby date

我有一个像"2011-06-02T23:59:59 + 05:30"这样的字符串.

我想将其转换为日期格式,只需要解析日期"2011-06-02".

Jit*_*its 27

对于Ruby 1.9.2:

require 'date'    # If not already required. If in Rails then you don't need this line).
puts DateTime.parse("2011-06-02T23:59:59+05:30").to_date.to_s
Run Code Online (Sandbox Code Playgroud)


rub*_*nce 12

require 'date'
d = Date.parse("2011-06-02T23:59:59+05:30")
d.strftime("%F")
Run Code Online (Sandbox Code Playgroud)

  • 这不会正确地解析时间戳 - 使用`DateTime.parse()`代替,除非你只想使用日期. (2认同)