Convert Seconds to Hours Minutes Milliseconds javaScript

Convert Seconds to Hours Minutes Milliseconds javaScript

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.`);
https://youtu.be/e_WoO3bgudE

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.
  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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *