简体   繁体   English

JS控制台和Rails控制台以不同的方式同时打印是正常的吗?

[英]Is it normal for js console and rails console to print the same time differently?

I have a date_time October 5th, 2000, 00:00. 我的date_time是2000年10月5日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. 使用js控制台和rails控制台进行打印将返回相同的前六位数字,但是js控制台在末尾添加了三个零。 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. 正如Tushar所说,javascript的Date.getTime返回毫秒。

You can see the reference for the Date class here: http://www.w3schools.com/jsref/jsref_obj_date.asp 您可以在此处查看Date类的参考: 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 从该页面获取Unix时间戳的方法并不明显,但显然在IE 8之后支持Date.now()函数: http : //www.ecma-international.org/ecma-262/5.1/#秒15.9.4.4

So for Javascript: 因此,对于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? Ruby对应的毫秒和第二个时间戳在这里详细说明: 如何在Ruby中将当前时间获取为13位整数?

To plagiarize the top answer there: 为了there窃这里的最佳答案:

require 'date'

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM