javascript javascript中的时间计算
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4583778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Time calculation in javascript
提问by Rajeev
starttime=(new Date()).getTime();
endtime=(new Date()).getTime();
(endtime-starttime )/1000
will give a value.What is this value and why is it divided by 1000
将给出一个值。这个值是什么以及为什么它除以 1000
采纳答案by Horia Dragomir
Well, in this particular case the value will be 0.
好吧,在这种特殊情况下,该值将为 0。
you need to divide it by 1000 because time is represented in miliseconds, so to get the seconds you need to perform the transformation 1s = 1000ms
您需要将其除以 1000,因为时间以毫秒表示,因此要获得执行转换所需的秒数 1s = 1000ms
回答by Tim Down
That code is calculating the number of seconds that have elapsed between two dates. The division by 1000 is there because the getTime()method returns a value measured in millseconds.
该代码正在计算两个日期之间经过的秒数。除以 1000 是因为该getTime()方法返回一个以毫秒为单位测量的值。
The code is actually needlessly long-winded. To get the milliseconds that have elapsed between two Dateobjects, you can just use the -operator on the Dates themselves:
该代码实际上是不必要的冗长。要获取两个Date对象之间经过的毫秒数,您可以-在Dates 本身上使用运算符:
var start = new Date();
// Some code that takes some time
var end = new Date();
var secondsElapsed = (end - start) / 1000;
回答by albertov
value=millisecond delta, it is divided to turn the delta into seconds
value=millisecond delta, 划分为将delta转为秒

