Open In App

JavaScript Program to Convert Hours into Minutes

Last Updated : 01 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Hours and Minutes are units of time. These units are essential for managing schedules, planning activities, and organizing daily routines. We are going to explore how we can convert hours into minutes using JavaScript.

Example:

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

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

Table of Content

Using Function

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

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

JavaScript
function convertHoursToMinutes(minutes) {
    return hours * 60;
}

const hours = 4;
const minutes = convertHoursToMinutes(hours);
console.log("minutes :", minutes);

Output
minutes : 240

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 convertToMinutes and will handle the conversion of hours to minutes. Instantiate the TimeConverter class using the new keyword, which creates an object of the class. Call the convertToMinutes method on the converter object, passing the number of hours you want to convert as an argument. Print result.

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

JavaScript
class TimeConverter {
    convertToMinutes(hours) {
        return hours * 60;
    }
}

const converter = new TimeConverter();
const hours = 4;
const minutes = converter.convertToMinutes(hours);
console.log("minutes:", minutes);

Output
minutes: 240

Time Complexity: O(1)

Space Complexity: O(1)


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads