convert minutes to seconds in javascript code example
Example 1: javascript convert seconds to minutes seconds
function convertHMS(value) {
const sec = parseInt(value, 10);
let hours = Math.floor(sec / 3600);
let minutes = Math.floor((sec - (hours * 3600)) / 60);
let seconds = sec - (hours * 3600) - (minutes * 60);
if (hours < 10) {hours = "0"+hours;}
if (minutes < 10) {minutes = "0"+minutes;}
if (seconds < 10) {seconds = "0"+seconds;}
return hours+':'+minutes+':'+seconds;
}
Example 2: how to convert minutes into seconds in javascript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>convert minutes to seconds in javascript</title>
</head>
<body>
<script type = "text/javascript">
function convertMinutestoSeconds(minutes)
{
return Math.floor(minutes * 60);
}
var minutesToSeconds = convertMinutestoSeconds(2);
document.write( "Result of converting minutes to seconds :- " + minutesToSeconds );
</script>
</body>
</html>
Example 3: convert seconds to minutes javascript
function secondsToHms(value) {
const sec = parseInt(value, 10);
let hours = Math.floor(sec / 3600);
let minutes = Math.floor((sec - hours * 3600) / 60);
let seconds = sec - hours * 3600 - minutes * 60;
if (hours < 10) { hours = '0' + hours; }
if (minutes < 10) { minutes = '0' + minutes; }
if (seconds < 10) { seconds = '0' + seconds; }
if (hours == 0) {
return +minutes + ':' + seconds;
} else {
return hours + ':' + minutes + ':' + seconds;
}
}