Convert Seconds to Hours Minutes Milliseconds javaScript

[ad_1]

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:

Ezoic

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:

Ezoic

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:

Ezoic

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.`);

Ezoic

Recommended Tutorials

  1. Compare two dates with JavaScript – Examples
  2. JavaScript Get Month Name With Various Examples
  3. Get Month 2 Digits in JavaScript
  4. javaScript Get Current Year 2 and 4 Digit – Example
  5. Get Current Date Time javaScript
  6. JavaScript: Date setUTCDate() Method
  7. Set UTC Month In javaScript
  8. setUTCFullYear() Method JavaScript With Examples
  9. javascript date setUTCHours() Method With Examples
  10. JavaScript: d.setUTCMinutes() Method e.g.Ezoic
  11. setUTCSecond() Method By JavaScript
  12. javaScript Date set UTC milliseconds
  13. setUTCSecond() Method By JavaScript
  14. JavaScript: Date.getUTCDate() Method
  15. getUTCmonth Method javaScript With Example
  16. Digital Clock with date in javaScript
  17. JavaScript: Set Date Methods
  18. JavaScript Get Date Methods

[ad_2]

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
Jassweb, Rai Chak, Punjab, India. 143518