Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptHow to get the timestamp in JavaScript ?

How to get the timestamp in JavaScript ?

In JavaScript, a timestamp is a numeric representation of the current time. It is a unique identifier that marks the exact moment when an event occurred or when a certain action was performed. This timestamp is useful in various applications such as logging, debugging, or measuring time intervals. In this article, we will explore different approaches to getting a timestamp in JavaScript.

Syntax: There are different ways to get a timestamp in JavaScript. Here is the syntax of some common methods:

Using Date.now() method: This method returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. It is a static method of the Date object and is widely supported by modern browsers.

var timestamp = Date.now();

 

Using new Date().getTime() method: This method also returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. It is an instance method of the Date object and is equivalent to calling the valueOf() method.

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

Using new Date().valueOf() method: This method returns the primitive value of a Date object, which is the number of milliseconds since January 1, 1970, 00:00:00 UTC.

var timestamp = new Date().valueOf();

Example 1: Using Date.now() method

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>Timestamp Example</title>
</head>
  
<body>
    <center>
        <h1 style="color:green">
            GeeksForGeeks
        </h1>
        <p id="demo"></p>
    </center>
  
    <script>
        var timestamp = Date.now();
        document.getElementById("demo").innerHTML 
            = "Timestamp: " + timestamp;
    </script>
</body>
  
</html>


Output:

 

Example 2: Using new Date().getTime() method

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>Timestamp Example</title>
</head>
  
<body>
    <center>
        <h1 style="color:green">
            GeeksForGeeks
        </h1>
        <p id="demo"></p>
    </center>
  
    <script>
        var timestamp = new Date().getTime();
        document.getElementById("demo").innerHTML 
            = "Timestamp: " + timestamp;
    </script>
</body>
  
</html>


Output:

 

Conclusion: In this article, we have discussed different approaches to getting a timestamp in JavaScript. We have seen how to use the Date.now() method, new Date().getTime() method, and new Date().valueOf() method to get a timestamp. The examples have shown how to use these methods in HTML code to display the timestamp on a webpage. Timestamps are useful in many programming scenarios, and JavaScript provides several easy-to-use methods to generate them.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments