시간을 초 단위로 변환하려면 먼저 현재 시간을 가져옵니다. 그런 다음 시간을 3600으로, 분을 60으로 곱합니다. 나머지는 아래에서 볼 수 있습니다 -
(hours*3600) + (min*60) + sec
예
다음 코드를 실행하여 현재 시간을 초 단위로 가져올 수 있습니다. −
<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> 출력
Seconds: 35 Minutes: 37 Hours: 9 Total seconds: 34655