Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Get Current Date and 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: 2020-06-18T12:00:07+05:30

641 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements