Open In App

JavaScript Program to Convert Minutes into Hours

Minutes and hours are units of time. These units are essential for managing schedules, planning activities, and organizing daily routines. These units are essential for managing schedules, planning activities, and organizing daily routines.

Formula Used:

Formula to convert minutes into hours :  1 minute = 1/60 hour

Below are the approaches for converting minutes into hours in JavaScript:

Table of Content

Using Function

In this approach, the code defines a function, convertMinutesToHours, that takes the minutes as an input parameter for converting it into an hour unit. It then invokes the function with a minute value of 180. Inside the function, use the formula: minute/60, to convert it into hour. Finally, return the result.

Example: The example below shows the demonstration of converting minutes into hours using a function.

function convertMinutesToHours(minutes) {
    return minutes / 60;
}

const minutes = 180;
const hours = convertMinutesToHours(minutes);
console.log("Hours:", hours);

Output
Hours: 3

Time Complexity: O(1)

Space Complexity: O(1)

Using Class

In this approach, we will define a class named TimeConverter which will contain a method called convertToHours and will handle the conversion of minutes to hours. Instantiate the TimeConverter class using the new keyword, which creates an object of the class. Call the convertToHours method on the converter object, passing the number of minutes you want to convert as an argument. Print result.

Example: The example below shows the demonstration of converting minutes into hours using class.

class TimeConverter {
    convertToHours(minutes) {
        return minutes / 60;
    }
}

const converter = new TimeConverter();
const minutes = 180;
const hours = converter.convertToHours(minutes);
console.log("Hours:", hours);

Output
Hours: 3

Time Complexity: O(1)

Space Complexity: O(1)

Article Tags :