If you want to work with time duration, you may need to convert it into minutes, seconds, and milliseconds. So, In this tutorial, we will show you how to convert seconds to hours, minutes, or milliseconds in JavaScript.
How to Convert Seconds to Hours Minutes Milliseconds javaScript
Here are some approaches to converting seconds to hours, minutes, and milliseconds:
- Approach 1 – Convert Seconds to Hours
- Approach 2 – Convert Seconds to Minutes
- Approach 3 – Convert Seconds to MilliSeconds
Approach 1 – Convert Seconds to Hours
To convert seconds to hours in JavaScript, you need to divide the number of seconds by 3600, considering that there are 60 seconds in a minute and 60 minutes in an hour. Here is a JavaScript function to convert seconds to hours:
function secondsToHours(seconds) {
// 1 hour = 60 minutes, 1 minute = 60 seconds
const hours = seconds / 3600;
return hours;
}
// Example usage:
const seconds = 3600; // Replace this with the desired number of seconds
const hoursResult = secondsToHours(seconds);
console.log(`${seconds} seconds is equal to ${hoursResult} hours.`);
Approach 2 – Convert Seconds to Minutes
To convert seconds to minutes in JavaScript, you need to divide the number of seconds by 60, as there are 60 seconds in a minute. Here is a JavaScript function to convert seconds to minutes:
function secondsToMinutes(seconds) {
// 1 minute = 60 seconds
const minutes = seconds / 60;
return minutes;
}
// Example usage:
const seconds = 120; // Replace this with the desired number of seconds
const minutesResult = secondsToMinutes(seconds);
console.log(`${seconds} seconds is equal to ${minutesResult} minutes.`);
Approach 3 – Convert Seconds to MilliSeconds
To convert seconds to milliseconds in JavaScript, you need to multiply the number of seconds by 1000, as there are 1000 milliseconds in a second. Here is a JavaScript function to convert seconds to milliseconds:
function secondsToMilliseconds(seconds) {
// 1 second = 1000 milliseconds
const milliseconds = seconds * 1000;
return milliseconds;
}
// Example usage:
const seconds = 30; // Replace this with the desired number of seconds
const millisecondsResult = secondsToMilliseconds(seconds);
console.log(`${seconds} seconds is equal to ${millisecondsResult} milliseconds.`);
Recommended Tutorials
- Compare two dates with JavaScript – Examples
- JavaScript Get Month Name With Various Examples
- Get Month 2 Digits in JavaScript
- javaScript Get Current Year 2 and 4 Digit – Example
- Get Current Date Time javaScript
- JavaScript: Date setUTCDate() Method
- Set UTC Month In javaScript
- setUTCFullYear() Method JavaScript With Examples
- javascript date setUTCHours() Method With Examples
- JavaScript: d.setUTCMinutes() Method e.g.
- setUTCSecond() Method By JavaScript
- javaScript Date set UTC milliseconds
- setUTCSecond() Method By JavaScript
- JavaScript: Date.getUTCDate() Method
- getUTCmonth Method javaScript With Example
- Digital Clock with date in javaScript
- JavaScript: Set Date Methods
- JavaScript Get Date Methods