How to get current date/time in seconds in JavaScript?


To convert time in seconds, firstly get the current time. Then, multiply the hours to 3600 and minutes to 60; rest you can see below −

(hours*3600) + (min*60) + sec

Example

You can try to run the following code to get the current time in seconds −

Live Demo

<html>
   <head>
      <title>JavaScript Get Seconds</title>
   </head>
   <body>
      <script>
         var dt = new Date();
         var sec = dt.getSeconds();
         document.write("Seconds: " + sec);
         var min = dt.getMinutes();
         document.write("<br>Minutes: " + min);
         var hrs = dt.getHours();
         document.write("<br>Hours: " + hrs);
         var total_seconds = (hrs*3600) + (min*60) + sec;
         document.write("<br>Total seconds: " + total_seconds) ;
      </script>
   </body>
</html>

Output

Seconds: 35
Minutes: 37
Hours: 9
Total seconds: 34655

Updated on: 18-Jun-2020

518 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements