Basically, I wanted to instantiate a Javascript Date object from a Ruby Date object via a JSON call. The problem is that Javascript's Date object constructor can't parse the default string output by Ruby's Date#to_s.
However, it can parse an integer which is milliseconds since the Unix epoch. Ruby's Date#to_i outputs the number of seconds since the unix epoch. So, using my mad arithmetic skillz, I figured I can create a Javascript Date with:
new Date(rubyGeneratedInteger * 1000);
So my usage of it went something like this. In my rails model, I generate an appropriate integer representation of the timestamp:I make that available to a JSON client through the controller:
And consume it from the Javascript client thus:
No comments:
Post a Comment