javascript minutes to milliseconds code example

Example 1: from milliseconds to hours in js

function msToTime(duration) {
  var milliseconds = parseInt((duration % 1000) / 100),
    seconds = Math.floor((duration / 1000) % 60),
    minutes = Math.floor((duration / (1000 * 60)) % 60),
    hours = Math.floor((duration / (1000 * 60 * 60)) % 24);

  hours = (hours < 10) ? "0" + hours : hours;
  minutes = (minutes < 10) ? "0" + minutes : minutes;
  seconds = (seconds < 10) ? "0" + seconds : seconds;

  return hours + ":" + minutes + ":" + seconds + "." + milliseconds;
}
console.log(msToTime(300000))

Example 2: convert milliseconds to minutes and seconds javascript

const millisToMinutesAndSeconds = (millis) => {
    var minutes = Math.floor(millis / 60000);
    var seconds = ((millis % 60000) / 1000).toFixed(0);
	//ES6 interpolated literals/template literals 
  	//If seconds is less than 10 put a zero in front.
    return `${minutes}:${(seconds < 10 ? "0" : "")}${seconds}`;
}

Example 3: what 1hr in milliseconds in javascript

/*
** Time in milliseconds

1 second = 1000 milliseconds
15 minutes = 900000 milliseconds
30 seconds = 30000 milliseconds	
1 minute = 60000 milliseconds
5 minutes = 300000 milliseconds
30 minutes = 1800000 milliseconds
45 minutes = 2700000 milliseconds
1 hour = 3600000 milliseconds

*/