# Convert Minutes to Hours and Minutes in JavaScript

Last updated: Mar 6, 2024
3 min

## #Convert Minutes to Hours and Minutes in JavaScript

To convert minutes to hours and minutes:

1. Get the number of full hours by dividing the minutes by `60`.
2. Get the remainder of the minutes.
3. Optionally, format the hours and minutes as `hh:mm`.
index.js
```Copied!```function toHoursAndMinutes(totalMinutes) {
const minutes = totalMinutes % 60;
const hours = Math.floor(totalMinutes / 60);

}

}

console.log(toHoursAndMinutes(90)); // ๐๏ธ 01:30
console.log(toHoursAndMinutes(60)); // ๐๏ธ 01:00
console.log(toHoursAndMinutes(115)); // ๐๏ธ 01:55
``````

We created a reusable function, which takes a number that represents the total minutes as a parameter and converts them to hours and minutes.

The first step is to get the number of full hours, by dividing the total minutes by `60` and rounding the result down.

The Math.floor() method rounds a number down if the number has a decimal, otherwise, it returns the number as is.

index.js
```Copied!```console.log(Math.floor(3.99)); // ๐๏ธ 3
console.log(Math.floor(3.01)); // ๐๏ธ 3
console.log(Math.floor(3)); // ๐๏ธ 3
``````
This ensures we don't get values with a decimal, e.g. `1.336` for the hours. If the value has a decimal, we want to show the minutes and hide the decimal.

We used the modulo (%) operator to get the remainder of the minutes.

index.js
```Copied!```console.log(90 % 60); // ๐๏ธ 30
``````

The example above shows that the remainder of dividing `90` by `60` is `30`, in other words, `1` full hour and `30` minutes.

The next step is to format the hours and minutes as `hh:mm`, e.g. `09:35`.

Our `padTo2Digits` function, takes care of adding a leading zero if the hours or minutes only contain a single digit (are less than `10`).

index.js
```Copied!```function padTo2Digits(num) {
}

``````

We want to make sure the result does not alternate between single and double digit values depending on the hours and minutes.

The example from the code snippet formats the values as `hh:mm`, but you could tweak this depending on your use case.

Here is another example.

index.js
```Copied!```function toHoursAndMinutes(totalMinutes) {
const minutes = totalMinutes % 60;
const hours = Math.floor(totalMinutes / 60);

return `\${hours}h\${minutes > 0 ? ` \${minutes}m` : ''}`;
}

console.log(toHoursAndMinutes(90)); // ๐๏ธ 1h 30m

console.log(toHoursAndMinutes(60)); // ๐๏ธ 1h

console.log(toHoursAndMinutes(115)); // ๐๏ธ 1h 55m
``````

The function displays the abbreviated values for the hours and minutes.

We used the ternary operator to display the value for the minutes only if they are more than `0`.

You can always display the minutes by removing the condition.

index.js
```Copied!```function toHoursAndMinutes(totalMinutes) {
const minutes = totalMinutes % 60;
const hours = Math.floor(totalMinutes / 60);

return `\${hours}h \${minutes}m`;
}

console.log(toHoursAndMinutes(60)); // ๐๏ธ 1h 0m
console.log(toHoursAndMinutes(90)); // ๐๏ธ 1h 30m
console.log(toHoursAndMinutes(115)); // ๐๏ธ 1h 55m
``````

## #Convert Minutes to Hours and Minutes without formatting

Alternatively, you can return an object containing the results from the conversion.

index.js
```Copied!```function toHoursAndMinutes(totalMinutes) {
const minutes = totalMinutes % 60;
const hours = Math.floor(totalMinutes / 60);

return {hours, minutes};
}

// ๐๏ธ { hours: 1, minutes: 30 }
console.log(toHoursAndMinutes(90));

// ๐๏ธ { hours: 1, minutes: 0 }
console.log(toHoursAndMinutes(60));

// ๐๏ธ { hours: 1, minutes: 55 }
console.log(toHoursAndMinutes(115));
``````

You can access the `hours` and `minutes` properties directly on the object that the function returns.

index.js
```Copied!```function toHoursAndMinutes(totalMinutes) {
const minutes = totalMinutes % 60;
const hours = Math.floor(totalMinutes / 60);

return {hours, minutes};
}

const result = toHoursAndMinutes(90);
console.log(result.hours); // ๐๏ธ 1
console.log(result.minutes); // ๐๏ธ 30
``````

Alternatively, you can destructure the values and assign them to variables in a single statement.

index.js
```Copied!```function toHoursAndMinutes(totalMinutes) {
const minutes = totalMinutes % 60;
const hours = Math.floor(totalMinutes / 60);

return {hours, minutes};
}

const {hours, minutes} = toHoursAndMinutes(90);
console.log(hours); // ๐๏ธ 1
console.log(minutes); // ๐๏ธ 30
``````