You are currently viewing 3 Best Ways to Get Current Time in Milliseconds | JavaScript

3 Best Ways to Get Current Time in Milliseconds | JavaScript

JavaScript Get Current Time in Milliseconds

When we building web applications, it is often necessary to keep track of the current time. Getting the current time in milliseconds can be important for a variety of reasons, such as calculating elapsed time, scheduling tasks, displaying a clock, etc. In JavaScript, we can get the current time using the Date object. However, the Date object provides the time in a specific format that may not be suitable for certain use cases. So, In this article, we will explore how to get the current time in milliseconds in JavaScript.

Advantage of Getting the current time in milliseconds

  • Measuring Time Intervals:If you are developing a game, you can calculate how long it takes a player to complete a level. By getting the current time in milliseconds at the start and end of the level, you can subtract the two values to determine the elapsed time.
  • Working with Dates: JavaScript provides several date and time-related functions that operate on milliseconds. We can calculate the difference in days between two dates, or add a specific number of hours to a date by getting current time with milliseconds.
  • Scheduling Tasks: We can schedule a task to occur in the future by getting current time with milliseconds. For example, you might want to display a message to a user after 10 seconds have passed since they last interacted with your application.

How to get current time in milliseconds

All of these methods return the number of milliseconds since January 1, 1970, 00:00:00 UTC. Because the current time in milliseconds since January 1, 1970, 00:00:00 UTC is a commonly used timestamp in computer systems, particularly in web development and other areas of software engineering. This timestamp, also known as the Unix time or POSIX time, represents the number of milliseconds that have elapsed since the Unix epoch (i.e., January 1, 1970, 00:00:00 UTC).

READ ALSO  3 Best Ways to Find Maximum Value in an Array in JavaScript

1. Using the Date.now() method

We can get the current time in milliseconds by using the Date object and its getTime() method.

const current = new Date().getTime();
console.log(current);

Output:

// 1682841080813
1682841089388
1682841096704
...
..
.
  • In this above code, we have created the variable ‘current’ and we create a new date object with the current date and time.
  • Then we use getTime() method of Date() and it returns the number of milliseconds since January 1, 1970
  • Finally, we logged the variabloe ‘current’.
  • We will see the current time in milliseconds printed to the console.

2. Using the Date.now() method

const current = Date.now();
console.log(current);
  • In this example code, we called the method Date.now(). It returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.
  • Then we assigned to the variable ‘current’ using the const keyword. This creates a new constant variable named current and sets its value to the current time in milliseconds.
  • Finally, we print the variable ‘current’ by using console.log() method. This logs the value of ‘current’ to the console. The current time in milliseconds is printed to the console.

3. Using the performance.now() method

const current = performance.now();
console.log(current);
  • Here, we have created the variable ‘current’ and assigend to called by performance.now() method
  • Then we print the variable ‘current’ by using console.log() method.
  • This method also return the current current time in milliseconds since January 1, 1970, 00:00:00 UTC.

Leave a Reply