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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:12:15  来源:igfitidea点击:

Time calculation in javascript

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转为秒

回答by mplungjan

DategetTime()gives the number of milliseconds since 1970 (Epoch)

Date getTime()出自1970 年(纪元)以来的毫秒数

Divide the difference by 1000 and you get seconds

将差值除以 1000 得到秒数