How do I calculate a duration time?
Asked Answered
U

7

31

I am developing a web-based application to capture start time and end time from system date-time but my main problem is that I don't know how can I get the duration time, between start and end time for downtime.

 //Function to get current start time
  var startTime = setInterval(function () { start() }, 1000);

  function start() {
    var startDate = new Date();
    var timeStart = startDate.toLocaleTimeString();
  $("#setCount").html(timeStart);
 }
Unwearied answered 19/3, 2013 at 7:12 Comment(0)
M
51

Do you mean this:

var date1 = new Date();
var date2 = new Date();
var diff = date2 - date1; //milliseconds interval

upd

check JSFiddle

Magocsi answered 19/3, 2013 at 7:17 Comment(2)
yes man ,but can i be able to get time that will start from the start of a downtime that will keep on counting until downtime finishes ?using your method ?Unwearied
don't really get what you need there, but added link to JSFiddle, hope it'l help youMagocsi
N
17

Update:

If you want to display the time difference to user, Serigo's method will do it. But if it is for any development purposes, below functions will make your life easy.


Just wanted to let you know about this console utility functions.

Put this line in top of your app initialization code console.time('appLifeTime');

Put this one in where ever you feel that your app ends. console.timeEnd('appLifeTime');

console.time('appLifeTime');

setTimeout(function delay(){
  console.timeEnd('appLifeTime');
}, 1500);

The above code piece will print, appLifeTime: 1500.583ms.

AFAIK, console.time & console.timeEnd works in firefox(with firebug) & webkit browsers(chrome, safari).

Nonanonage answered 19/3, 2013 at 11:11 Comment(0)
D
9

To simply measure time elapsed, use Date.getTime() which outputs current time in milliseconds since unix epoch.

You can substract one millis value from another to get the duration.

Example:

var startTime = new Date().getTime();

setTimeout(function () {
  var endTime = new Date().getTime();
  console.log("duration [ms] = " + (endTime-startTime));
}, 1500);

Output would be, of course: duration [ms] = 1500 (or couple ms less or more).

Depositor answered 19/3, 2013 at 7:16 Comment(0)
D
7

I have this function to show the time distance between two dates:

export const timeDistance = (date1, date2) => {
  let distance = Math.abs(date1 - date2);
  const hours = Math.floor(distance / 3600000);
  distance -= hours * 3600000;
  const minutes = Math.floor(distance / 60000);
  distance -= minutes * 60000;
  const seconds = Math.floor(distance / 1000);
  return `${hours}:${('0' + minutes).slice(-2)}:${('0' + seconds).slice(-2)}`;
};

The output in the format h:mm:ss hours can grow arbitrary.

Dulcine answered 9/10, 2019 at 7:59 Comment(0)
P
1

Use this nifty jQuery plugin: http://timeago.yarp.com/

Timeago is a jQuery plugin that makes it easy to support automatically updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago"). Download, view the examples, and enjoy.

You opened this page about a minute ago. (This will update every minute. Wait for it.)

This page was last modified 13 days ago.

Ryan was born 34 years ago.

Possing answered 19/3, 2013 at 7:15 Comment(0)
Z
0
**Scripts:**

  function formatDate(difference) {
  
     //Arrange the difference of date in days, hours, minutes, and seconds format
     let days = Math.floor(difference / (1000 * 60 * 60 * 24));
     let hours = Math.floor((difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
     let minutes = Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60));
     let seconds = Math.floor((difference % (1000 * 60)) / 1000);
     return "Total time elapsed is: " + days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds.";
  }
  let start = new Date("December 31, 2020 23:59:59");
  let end = new Date();
  let difference = end - start;
  formatDate(difference);
Zoosporangium answered 5/2, 2024 at 9:22 Comment(0)
Z
0

let output = document.getElementById('output');
      function formatDate(difference) {
      
         //Arrange the difference of date in days, hours, minutes, and seconds format
         let days = Math.floor(difference / (1000 * 60 * 60 * 24));
         let hours = Math.floor((difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
         let minutes = Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60));
         let seconds = Math.floor((difference % (1000 * 60)) / 1000);
         output.innerHTML += "Total time elapsed is: " + days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds.";
      }
      let start = new Date("December 31, 2020 23:59:59");
      let end = new Date();
      let difference = end - start;
      formatDate(difference);
<h3> Using the <i> Date() object </i> to find the time elapsed in JavaScript </h3>
   <div id="output"></div>
Zoosporangium answered 5/2, 2024 at 9:24 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.