LoginSignup
0
0

More than 5 years have passed since last update.

Time stamp difference between AngularJS and Rails

Posted at

Recently, I've been working on Rails and AngularJS with CoffeeScript, and found difficulty to pass date-time data type object from Rails, where those objects are stocked, to AngularJS, which is to sort and some stuff.

My solution is this:


In controller.rb;
@time = Record.find(1).created_at

In view.html.erb;
ng-init="time = @time.to_i * 1000"
{{ time | date:'yyyy/MM/dd' }}


Log in console
cre = Record.first.created_at
=> Thu, 21 May 2015 02:07:56 UTC +00:00
cre.to_time
=> 2015-05-21 11:07:56 +0900
cre.to_time.class
=> Time
cre.to_time.to_i
=> 1432174076
cre.to_time.to_i.class
=> Fixnum


Values in '.rb' files, it's related with Active Record, but once passed into '.html.erb', those are converted to usual data.

Because of above, if you check in console, you have to change that to Time Class Object.

Rails -> Date Object( --second ) || Time Object( --miliseconds )
AngularJS -> Timestamp == Rails.Time


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0