Time conversion between ruby on rails and javascript vice versa?
From jquery to rails:
"Wed Mar 12 2014 23:45:39 GMT+0530 (IST)".to_time
Perhaps the most reliable way is to use seconds since the epoch for ruby, and milliseconds for JavaScript.
In ruby:
t = Time.now
# => 2014-03-12 11:18:29 -0700
t.to_f * 1000 # convert to milliseconds since 1970-01-01 00:00:00 UTC.
# => 1394648309130.185
This value can be directly given to the JavaScript Date constructor:
var d = new Date(1394648309130.185)
d // Wed Mar 12 2014 11:18:29 GMT-0700 (Pacific Daylight Time)
d.getTime() // 1394648309130 (Fractions of a millisecond are dropped)
The output of d.getTime()
divided by 1000 can be given to ruby's Time.at()
:
Time.at( 1394648309130 / 1000.0 )
# => 2014-03-12 11:18:29 -0700