How do you get a timestamp in JavaScript?

Front End TechnologyJavascriptWeb Development

In this tutorial, we will learn to get a timestamp in JavaScript. It never happens that software developers or application developers don’t require to play with date and time. In many situations, they are required to play with date and timestamp. For example, to show the time on the user’s dashboard, save the time of an operation in the database.

Now, let’s understand what the timestamp is? The timestamp is the total number of milliseconds from the 1st January 1970 from when UNIX epochs begin. Using the timestamp, we can extract the date, day, year, month, hours, minutes, second, milliseconds, etc.

So, now users can understand why timestamp is important. Rather than storing the whole string of dates and times, it is better to store the timestamp, and we can extract the perfect date and time when we need it. So, it saves space in the database.

Here, we have different methods to get the timestamp using the built-in library methods of vanilla JavaScript and Moment JS library of JavaScript.

Using the Date.now() or getTime() Methods

In vanilla JavaScript, we have a built-in Date class to get date and time. Users can directly use the Date.now() method of the Date class, which returns the total number of milliseconds from UNIX epochs has been started. Also, in the same way, users can create the object of the Date class and use the getTime() method with the object.

Syntax

Here is the syntax to use the Date.now() method and getTime() method with the object of the Date class.

  • To use the now() method of the Date class.
let timestamp = Date.now();
  • To use the getTime() method.
let date = new Date(); // object of the date class
timestamp = date.getTime(); // To get the timestamp

Example

In the below example, we are simply getting the timestamp using the Date.now() method. Also, we have created the object of the Date class and applied invoked getTime() method using that object.

<!DOCTYPE html>
<html>
<body>
   <h2> Get a timestamp in JavaScript. </h2>
   <h4> Get timestamp using the Date.now() </h4>
   <div id="timestamp1"> </div>
   <h4> Get timestamp using the getTime() method </h4>
   <div id="timestamp2"> </div>
   <script>
      let timestamp1 = document.getElementById("timestamp1");
      let timestamp2 = document.getElementById("timestamp2");
      let timestamp = Date.now();
      timestamp1.innerHTML = timestamp;
      let date = new Date(); // creating the object of the date class
      timestamp = date.getTime();
      timestamp2.innerHTML = timestamp;
   </script>
</body>
</html>

Using the Moment JS valueof() method

The Moment JS is the JavaScript library with the extra cool features to manipulate the date and time. Here, we will use the valueof() method to get the total milliseconds from UNIX epochs has been started, and to get the total seconds, we will use the unix() method.

Before using this approach, we need to ensure that either we have installed the Moment Js library in our application or we are using the CDN to embed the Moment JS with our code.

Syntax

let timestamp = moment().valueof(); // timestamp in milliseconds.
let timestamp = moment().unix(); // timestamp in seconds.

Example

The below example demonstrates the use of the valueof() method of the Moment JS library. We are getting the current timestamp in the milliseconds using the valueof() method. Moreover, we have also implemented the unix() method of Moment JS to get the timestamp in milliseconds.

<!DOCTYPE html>
<html>
<head>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment-with-locales.min.js" integrity="sha512- vFABRuf5oGUaztndx4KoAEUVQnOvAIFs59y4tO0DILGWhQiFnFHiR+ZJfxLDyJlXgeut9Z07Svuvm+1Jv89w5g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
</head>
<body>
   <h2> Get a timestamp in JavaScript. </h2>
   <h4> Get timestamp in milliseconds using the valueOf() method of Moment JS. </h4>
   <div id="timestamp1"> </div>
   <h4> Get timestamp in seconds using the valueOf() method of Moment JS.</h4>
   <div id="timestamp2"> </div>
   <script>
      let timestamp1 = document.getElementById("timestamp1");
      let timestamp2 = document.getElementById("timestamp2");
      let timestamp = moment().valueOf(); // timestamp in milliseconds.
      timestamp1.innerHTML = timestamp;
      timestamp = moment().unix(); // timestamp in seconds
      timestamp2.innerHTML = timestamp;
   </script>
</body>
</html>

Use of the Timestamp

There can be various uses of the timestamp in JavaScript, but here, we have shown the best use with an example. We can use the timestamp to find the difference between two dates in years, months, days, or whatever unit you want.

Syntax

Users can follow the below syntax to find the difference between two timestamps.

let startDate = new Date("01/23/1990"); // creating date object
let diff = Date.now() - startDate.getTime(); // difference between current date and start date

Example

In the below example, we have find the number of years, days, and weeks between two dates using the timestamp.

<html>
<body>
   <h2> Get a timestamp in JavaScript. </h2>
   <h4>Number of years between the 01 / 23 / 1990 and current date. </h4>
   <div id="timestamp1"> </div>
   <h4>Difference in weeks</h4>
   <div id="timestamp2"> </div>
   <h4> Difference in days</h4>
   <div id="timestamp3"> </div>
   <script>
      let timestamp1 = document.getElementById("timestamp1");
      let timestamp2 = document.getElementById("timestamp2");
      let timestamp3 = document.getElementById("timestamp3");
      let startDate = new Date("01/23/1990");
      let diff = Date.now() - startDate.getTime();
      let day = 1000 * 60 * 60 * 24; // milliseconds of 1 day
      timestamp3.innerHTML = Math.floor(diff / day);
      let week = 7 * day // 7 days is equal to one week.
      timestamp2.innerHTML = Math.floor(diff / week);
      let year = 365 * day; // year has 365 days
      timestamp1.innerHTML = Math.floor(diff / year);
   </script>
</body>
</html>

Conclusion

We have seen how to get the timestamp using the Moment JS library methods and the Date() class of JavaScript. Also, we have seen the uses of the timestamp via a single example with the working code.

raja
Updated on 25-Jul-2022 08:39:25

Advertisements