简体   繁体   English

我在这里有什么时间?

[英]what measure of time do i have here?

If the answer is 8. What is 8? 如果答案是8。什么是8? milliseconds? 毫秒? or must I (time/100)-(ntime/100) to get milliseconds? 还是我必须(time / 100)-(ntime / 100)来获取毫秒?

var time=(+new Date());

for(var i=0;i<100;i++){/*something intensive*/}

var ntime=(+new Date());

console.log('answer: '+((ntime)-time)+('( '+time+' , '+ntime+' )'));

answer: 8 / 1404573120333 >> 1404573120341 答案:8/1404573120333 >> 1404573120341

(+new Date()) A unix timestamp is described as the time in seconds since the epoch (+ new Date())Unix时间戳记被描述为从纪元以来的时间(以单位)

1404573825 would be epoch, seconds since jan1,1970. 1404573825将是一个纪元,距1970年1月1日以来的秒数。

1404573120333 is 3 digits longer, most likely milliseconds since jan1,1970 1404573120333长​​3位,最可能是自1970年1月1日以来的毫秒

difference therefore too. 因此也是如此。

When turning the Date value into a number using the + operator, it will call the valueOf method . 使用+运算符将Date值转换为数字时,它将调用valueOf方法 The value returned is the number of milliseconds since 1970-01-01 UTC. 返回的值是自1970-01-01 UTC以来的毫秒数。

"The valueOf method returns the primitive value of a Date object as a number data type, the number of milliseconds since midnight 01 January, 1970 UTC." “ valueOf方法将Date对象的原始值作为数字数据类型返回,即自UTC 1970年1月1日午夜以来的毫秒数。”

The valueOf method returns the same value as the getTime method . valueOf方法返回与getTime方法相同的值。 The doumentation for the getTime method has this example for measuring time in milliseconds: getTime方法的加倍有以下示例,以毫秒为单位测量时间:

var end, start;

start = new Date();
for (var i = 0; i < 1000; i++)
  Math.sqrt(i);
end = new Date();

console.log("Operation took " + (end.getTime() - start.getTime()) + " msec");

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

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