How do you get a timestamp in JavaScript

How do you get a timestamp in JavaScript?

Last Updated by Jai 05-May-20

0 508


In this tutorial, we will see how to Get Timestamp in JavaScript. To get the current timestamp in JavaScript we can use 5 different methods as Date.now() method, Valueof() method, getTime() method, unary plus, number constructor.

1. Date.now() method:

You can simply use the JavaScript Date.now() method to generate the UTC timestamp in milliseconds. date display in terms of milliseconds since midnight Jan 1, 1970.

Program:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Using Date.now() Method</title>
</head>
<body>
  <h1>Using Date.now() Method</h1>
   <script>
     // Creating a timestamp
     var timedisplay = Date.now();
     document.write(timedisplay + "<hr>");
     // We Converting it to human-readable date and time
     var d = new Date(timedisplay);
     document.write(d);
  </script>
</body>
</html>

Output:

How do you get a timestamp in JavaScript?


2. Valueof() Method

Valueof() Method is an alternative option that returns the primitive value of a Date object that has the same quantity of milliseconds since the Unix Epoch.

Program:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Using Valueof() Method</title>
</head>
<body>
  <h1>Using Valueof() Method</h1>
   <script>
     // Creating a timestamp
     var timedisplay = new Date().valueOf();
     document.write(timedisplay + "<hr>");
     // We Converting it to human-readable date and time
     var d = new Date(timedisplay);
     document.write(d);
   </script>
</body>
</html>

Output:

How do you get a timestamp in JavaScript?


3. getTime() Method:

getTime() method give the option to implementing the getTime() method for getting UNIX timestamp that is equivalent to the value0f() method.

Program:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Using getTime() Method</title>
</head>
<body>
  <h1>Using getTime() Method</h1>
   <script>
     // Creating a timestamp
     var timedisplay = new Date().getTime();
     document.write(timedisplay + "<hr>");
     // We Converting it to human-readable date and time
     var d = new Date(timedisplay);
     document.write(d);
   </script>
</body>
</html>

Output:

How do you get a timestamp in JavaScript?


4. Unary plus

Unary plus operator gives you option which converts the Date object into milliseconds. 

It works by calling the valueOf() method of the Date object.

Program:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>UsingUnary plus operator  Method</title>
</head>
<body>
   <h1>Using Unary Plus Operator Method</h1>
    <script>
     // Creating a timestamp
      var timedisplay = +new Date();
      document.write(timedisplay + "<hr>");
     // We Converting it  to human-readable date and time
      var d = new Date(timedisplay);
      document.write(d);
    </script>
</body>
</html>

Output:

How do you get a timestamp in JavaScript?


5. Number Constructor

Number Constructor used to get a time value as a number of data time.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Using number date time Method</title>
</head>
<body>
  <h1>Using Number Date Time Method</h1>
   <script>
      // Creating a timestamp
      var timedisplay = Number(new Date());
      document.write(timedisplay + "<hr>");
      // We Converting it  to human-readable date and time
      var d = new Date(timedisplay);
      document.write(d);
  </script>
</body>
</html>

Output:

How do you get a timestamp in JavaScript?


Note:

You also find out Get Timestamp using jQuery and Moment.js.


Share:


Leave a Reply


Comments
    Waiting for your comments