pmichna
pmichna

Reputation: 4888

How to parse date with time zone from a custom string?

In cookies I store a date returned by JS new Date() in the following format:

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

How can I parse this in Rails to get a DateTime object with correct time zone? .to_datetime returns 2015-10-29T15:46:19+00:00 which has incorrect time zone.

Upvotes: 0

Views: 770

Answers (1)

shirakia
shirakia

Reputation: 2409

Normally JS new Date returns like this.

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

I guess your JS datetime string is missing + after GMT. This works perfectly

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

Upvotes: 1

Related Questions