I have a date_time October 5th, 2000, 00:00. Printing it with the js console and the rails console return the same first six digits, but then the js console adds three zeros at the end. Should this be expected behavior?
var date = new Date(2000, 10, 5);
date.getTime();
=> 970722000000
Date.new(2000,10,5).to_time.to_i
=> 970722000
As Tushar said, javascript's Date.getTime returns milliseconds.
You can see the reference for the Date class here: http://www.w3schools.com/jsref/jsref_obj_date.asp
It's not obvious how to get a Unix timestamp from that page, but apparently there is a Date.now() function that is supported post IE 8: http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.4.4
So for Javascript:
Date.now() //seconds - this doesn't seem to work, despite what Google says
Math.floor(new Date().getTime() / 1000) //so for seconds you're probably stuck with this
Date.getTime() //milliseconds
The corresponding millisecond and second timestamps for Ruby are elaborated here: How to get the current time as 13-digit integer in Ruby?
To plagiarize the top answer there:
require 'date'
p DateTime.now.strftime('%s') # "1384526946" (seconds)
p DateTime.now.strftime('%Q') # "1384526946523" (milliseconds)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.