我的日期时间是2000年10月5日00:00。使用js控制台和rails控制台打印它会返回相同的前六位数,但随后js控制台在末尾添加三个零。这应该是预期的行为吗?
var date = new Date(2000, 10, 5);
date.getTime();
=> 970722000000
Date.new(2000,10,5).to_time.to_i
=> 970722000
答案 0 :(得分:1)
正如Tushar所说,javascript的Date.getTime返回毫秒。
您可以在此处查看Date类的参考:http://www.w3schools.com/jsref/jsref_obj_date.asp
如何从该页面获取Unix时间戳并不明显,但显然在IE 8后支持Date.now()函数:http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.4.4
所以对于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
Ruby的相应毫秒和秒时间戳在这里详细说明:How to get the current time as 13-digit integer in Ruby?
抄袭那里的最佳答案:
require 'date'
p DateTime.now.strftime('%s') # "1384526946" (seconds)
p DateTime.now.strftime('%Q') # "1384526946523" (milliseconds)