【问题标题】:Is it normal for js console and rails console to print the same time differently?js控制台和rails控制台在同一时间打印不同是否正常?
【发布时间】:2015-10-30 03:21:02
【问题描述】:

我有一个 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 

【问题讨论】:

  • 它们没有区别,都是一样的,只是JS时间是以毫秒为单位的

标签: javascript ruby-on-rails datetime time console


【解决方案1】:

正如 Tushar 所说,javascript 的 Date.getTime 返回毫秒数。

您可以在此处查看 Date 类的参考:http://www.w3schools.com/jsref/jsref_obj_date.asp

如何从该页面获取 Unix 时间戳并不明显,但显然有一个 Date.now() 函数在 IE 8 之后受支持: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)

【讨论】:

  • 你确定Date.now()?对我来说返回“Uncaught TypeError: Date.now is not a function(...)”。
  • 是的,你是对的,它甚至在我的浏览器中都不起作用(它只为我返回毫秒)。更新
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-11
  • 2011-02-23
  • 1970-01-01
  • 1970-01-01
  • 2015-10-28
  • 1970-01-01
相关资源
最近更新 更多