1

With JS I am calculating the elapsed time as follows:

start = new Date().toISOString();
end = new Date().toISOString();
var time = last1 + ' - ' + last2;

For example, the above gives me to strings concatenated like this:

 '2020-04-09T13:15:52.838Z - 2020-04-09T13:16:09.704Z'

What is the correct way of getting the a delta value that represents the amount of time in seconds elapsed between start and end

aywoki
  • 189
  • 8
  • 2
    Given Date objects, `date1 - date2` will give you the difference in milliseconds, divide by 1,000 to get seconds. – RobG Mar 10 '21 at 00:18

1 Answers1

0

You can use moment#diff:

const diff = (start, end) =>
  moment(end).diff(moment(start), 'seconds');
console.log( diff('2021-03-09T22:47:54.392Z', new Date().toISOString()) );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous"></script>
Majed Badawi
  • 16,831
  • 3
  • 12
  • 27
  • Please don't recommend a Moment-based solution when the question was not asking about Moment. Please see also https://momentjs.com/docs/#/-project-status/ – Matt Johnson-Pint Mar 10 '21 at 01:07