3
 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

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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;

Comments

0

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

Comments

0

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

Divide the difference by 1000 and you get seconds

2 Comments

you mean epoch, and new Date() returns the current time, not the epoch by default.
new Date().getTime() does. Fixed era

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.