Open In App

Finding the time elapsed in JavaScript

Last Updated : 06 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to find out the time elapsed between the two-time instances using the Date object of JavaScript. So firstly we will see what is Date object in JavaScript in short.

Date Object: A JavaScript Date object deal with dates and times and it is a built-in object. It stores a value in milliseconds for the Unix Timestamp which is nothing but milliseconds passed since 1 January 1970 UTC. We can use it to create a new date, format a date, get elapsed time between two different time values, etc.

So now we will discuss how to find out the time elapsed between two different time values.

Example 1: Time elapsed for the execution of the function.

Approach:

  • Assign the current time before the task starts executing to the start variable.
  • Do a task for which we want to calculate the time elapsed, here our task is to run the greeting() function.
  • After completing the execution of that function whatever the time is, just assign it to the variable end.
  • By subtracting the start from the end (end-start) we will get the time elapsed for the given function.

Javascript




// Using Date objects
let start = Date.now();
  
// Task for which we want to calculate the 
// time taken / time elapsed from the start 
// of the process to end of that process
  
function greeting(){
    console.log("Hey Geeks");
}
greeting();
  
// get the end time
let end = Date.now();
  
// elapsed time in milliseconds
let elapsed = end - start;   
  
// converting milliseconds to seconds 
// by dividing 1000
console.log(elapsed/1000);


Output

Hey Geeks
0.003

We calculated the time elapsed for the function by subtracting the end time and start time of the greeting function by noticing the start time of that function and the end time of that function.

Example 2: Time elapsed for the two-time instances, set by the user.

1. Finding the time elapsed between past date/time and current date/time:

Approach:

  • Define the past date into the Date object and assign it to a past variable.
  • Declare the now variable and assign the current time to it.
  • When we do not define any time to Date object it takes current time.
  • By subtracting the past from now (now-past) we will get the time elapsed for the given time instances.

Javascript




// Calculating the time elapsed from 
// 1970-01-01 to up to now
  
// set the time
let past = new Date('1970-01-01');
  
// assigning present time to now variable
let now = new Date();
  
let elapsed = (now - past);
  
// by dividing by 1000 we will get 
// the time in seconds
console.log(elapsed / 1000);


Output

1662201865.404

2. Finding the time elapsed between two dates/times defined by the user :

Approach:

  • Define the first date in the start variable.
  • Define the second date into the end variable.
  • Simply subtract these two-time instances we will get the time elapsed between these two instances.

Javascript




// Calculating the time elapsed from 
// 1970-01-01 to 2020-08-22
  
// set the time
let first = new Date('1970-01-01'); 
  
// assigning present time to now variable
let end = new Date('2020-08-22');    
  
let elapsed = (end-first);
  
// We'll get the elapsed time in days
console.log(elapsed/(1000*60*60*24));


Output

18496

By default, we will get the time elapsed in milliseconds so by dividing 1000. We’ll get it into seconds and like this, we converted it into days.

Likewise, we can define every instance of the Date/Time, and accordingly, we can find out the time elapsed. Here we discussed defining the DD:MM:YY format. You can refer to the JavaScript Set Date Methods article on GeeksforGeeks for setting the dates in different formats.

I hope now you can find out the time elapsed by setting the time whatever you want. Everything in these operations time will be in milliseconds so according to our need, we have to convert it.



Like Article
Suggest improvement
Share your thoughts in the comments