Javascript time calculation

 starttime=(new Date()).getTime();
 endtime=(new Date()).getTime();

 (endtime-starttime )/1000 

will give value. What is this value and why is it divided by 1000

+3
source share
4 answers

Well, in this particular case, the value will be 0.

you need to divide it by 1000 because the time is in milliseconds, so to get the seconds you need to do the conversion 1s = 1000ms

+2
source

This code calculates the number of seconds elapsed between two dates. The division by 1000 occurs because the method getTime()returns a value measured in milliseconds.

The code is actually unnecessarily long. To get the milliseconds that have elapsed between two objects Date, you can simply use the operator -for yourself Date:

var start = new Date();

// Some code that takes some time

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

Date getTime() 1970 ()

1000,

0

value = millisecond delta, it is divided by delta for seconds

0
source

Source: https://habr.com/ru/post/1783198/


All Articles