我的date_time是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

最佳答案

正如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?

为了there窃这里的最佳答案:

require 'date'

p DateTime.now.strftime('%s') # "1384526946" (seconds)
p DateTime.now.strftime('%Q') # "1384526946523" (milliseconds)

07-24 09:50
查看更多