【问题标题】:How to parse date with time zone from a custom string?如何从自定义字符串中解析带有时区的日期?
【发布时间】:2015-10-29 23:38:44
【问题描述】:

在 cookie 中,我以以下格式存储由 JS new Date() 返回的日期:

Thu Oct 29 2015 15:46:19 GMT 0100 (CET)

如何在 Rails 中解析它以获取具有正确时区的 DateTime 对象? .to_datetime 返回时区错误的2015-10-29T15:46:19+00:00

【问题讨论】:

标签: ruby-on-rails ruby datetime


【解决方案1】:

通常 JS new Date 是这样返回的。

(new Date).toString(); // => "Fri Oct 30 2015 00:36:43 GMT+0900 (JST)"

我猜你的 JS 日期时间字符串在 GMT 之后丢失了 +。 这完美无缺

'Thu Oct 29 2015 15:46:19 GMT+0100 (CET)'.to_datetime # => Thu, 29 Oct 2015 15:46:19 +0100

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多