开发者

Time calculation in javascript

开发者 https://www.devze.com 2023-02-02 18:59 出处:网络
starttime开发者_开发技巧=(new Date()).getTime(); endtime=(new Date()).getTime(); (endtime-starttime )/1000
 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


Well, in this particular case the value will be 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


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.

The code is actually needlessly long-winded. To get the milliseconds that have elapsed between two Date objects, you can just use the - operator on the Dates themselves:

var start = new Date();

// Some code that takes some time

var end = new Date();
var secondsElapsed = (end - start) / 1000;


value=millisecond delta, it is divided to turn the delta into seconds


Date getTime() gives the number of milliseconds since 1970 (Epoch)

Divide the difference by 1000 and you get seconds

0

精彩评论

暂无评论...
验证码 换一张
取 消