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
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
Share Improve this question asked Jan 3, 2011 at 10:44 RajeevRajeev 47k80 gold badges191 silver badges288 bronze badges4 Answers
Reset to default 2Well, 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 Date
s 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