How to get the current date and time in seconds?

Method 1: Using Date.now() method: The Date.now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC. This is known as the epoch time. It may be used for timestamping as an order of events could easily be checked by comparing the timestamps.

The returned milliseconds can be converted to seconds by dividing the value by 1000 and then using the Math.round() function to round the value. This is done to prevent inconsistencies due to the float values.

Syntax:

Date.now()

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
      
<head>
    <title>
        How to get current date in JavaScript?
    </title>
</head>
  
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
      
    <b>
        How to get current date in JavaScript?
    </b>
      
    <p>
        Current date/time in milliseconds is: 
        <span class="output-msecs"></span>
    </p>
      
    <p>
        Current date/time in seconds is: 
        <span class="output-secs"></span>
    </p>
      
    <p>
        Current date/time in words is: 
        <span class="output-words"></span>
    </p>
  
    <button onclick="getCurrentDate()">
        Get current time
    </button>
      
    <script type="text/javascript">
      
        function getCurrentDate() {
            dateInMillisecs = Date.now();
  
            // rounding the value to prevent inconsistencies
            // due to floating points
            dateInSecs = Math.round(dateInMillisecs / 1000);
  
            dateInWords = new Date(dateInMillisecs);
  
            document.querySelector('.output-msecs').textContent
                    = dateInMillisecs;
                      
            document.querySelector('.output-secs').textContent
                    = dateInSecs;
                      
            document.querySelector('.output-words').textContent
                    = dateInWords;
        }
    </script>
</body>
  
</html>                    

chevron_right


Output:
now-output

Method 2: Using new Date.getTime() method: The Date.getTime() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC. It always uses the UTC for time representation. It has to be initialized with the new keyword, unlike the Date.now() method.

The returned milliseconds can be converted to seconds by dividing the value by 1000 and then using the Math.round() function to round the value. This is done to prevent inconsistencies due to the float values.

Syntax:

new Date().getTime()

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
      
<head>
    <title>
        How to get current date in JavaScript?
    </title>
</head>
  
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
      
    <b>
        How to get current date in JavaScript?
    </b>
      
    <p>
        Current date/time in milliseconds is: 
        <span class="output-msecs"></span>
    </p>
      
    <p>
        Current date/time in seconds is: 
        <span class="output-secs"></span>
    </p>
      
    <p>
        Current date/time in words is: 
        <span class="output-words"></span>
    </p>
  
    <button onclick="getCurrentDate()">
        Get current time
    </button>
      
    <script type="text/javascript">
      
        function getCurrentDate() {
            dateInMillisecs = new Date().getTime();
  
            // Rounding the value to prevent inconsistencies
            // due to floating points
            dateInSecs = Math.round(dateInMillisecs / 1000);
  
            dateInWords = new Date(dateInMillisecs);
  
            document.querySelector('.output-msecs').textContent
                    = dateInMillisecs;
                      
            document.querySelector('.output-secs').textContent
                    = dateInSecs;
                      
            document.querySelector('.output-words').textContent
                    = dateInWords;
        }
    </script>
</body>
  
</html>                    

chevron_right


Output:
getTime-output



My Personal Notes arrow_drop_up

Technical Writer Lets have an update

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.