Home ยป JavaScript ยป Convert Minutes to Days Hours Minutes in JavaScript

Convert Minutes to Days Hours Minutes in JavaScript

To convert minutes to days, hours, and minutes in JavaScript, you can use the following steps:

  1. Divide the number of minutes by the number of minutes in a day (1440) to get the number of days.
  2. Use the modulo operator (%) to find the number of minutes remaining after the number of days has been calculated.
  3. Divide the remaining number of minutes by the number of minutes in an hour (60) to get the number of hours.
  4. Use the modulo operator again to find the number of minutes remaining after the number of hours has been calculated.

Convert Minutes to Days Hours Minutes in JavaScript Example

Here's an example of how to implement these steps in JavaScript:

function convertMinutes(minutes) {
  // Calculate the number of days
  const days = Math.floor(minutes / 1440);

  // Calculate the number of hours
  const hours = Math.floor((minutes % 1440) / 60);

  // Calculate the number of minutes
  const remainingMinutes = minutes % 60;

  return `${days} days, ${hours} hours, ${remainingMinutes} minutes`;
}

// Test the function
console.log(convertMinutes(1440)); // Output: "1 days, 0 hours, 0 minutes"
console.log(convertMinutes(1510)); // Output: "1 days, 1 hours, 10 minutes"
console.log(convertMinutes(7200)); // Output: "2 days, 12 hours, 0 minutes"
console.log(convertMinutes(7201)); // Output: "2 days, 12 hours, 1 minutes"

In this example, we define a function convertMinutes that takes a number of minutes as input and returns a string with the equivalent number of days, hours, and minutes. The function first calculates the number of days by dividing the number of minutes by the number of minutes in a day (1440). Then it calculates the number of hours by dividing the remaining number of minutes by the number of minutes in an hour (60). Finally, it calculates the number of minutes remaining after the number of days and hours have been calculated.

Related:

  1. JavaScript Program to Display Current Month Calendar
  2. What are Array Methods in JavaScript?