Open In App

Convert minutes to hours/minutes with the help of JQuery

The task is to convert the given minutes to Hours/Minutes format with the help of JavaScript. Here, 2 approaches are discussed. 

Approach 1:



Example 1: This example implements the above approach. 




<head>
    <script src=
    </script>
</head>
<body>
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
    <p id="GFG_UP">
    </p>
    Type Minutes:
    <input class="mins" />
    <br>
    <br>
    <button onclick="GFG_Fun()">
        click here
    </button>
    <p id="GFG_DOWN">
    </p>
    <script>
        var up = document.getElementById('GFG_UP');
        var element = document.getElementById("body");
        up.innerHTML =
        "Click on the button to get minutes in Hours/Minutes format.";
         
        function GFG_Fun() {
            // Getting the input from user.
            var total = $('.mins').val();
            // Getting the hours.
            var hrs = Math.floor(total / 60);
            // Getting the minutes.
            var min = total % 60;
            $('#GFG_DOWN').html(hrs +
                    " Hours and " + min + " Minutes");
        }
    </script>
</body

Output:



Convert minutes to hours/minutes with the help of JQuery

Approach 2:

Example: This example implements the above approach. 




<head>
    <script src=
    </script>
</head>
<body>
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
    <p id="GFG_UP">
    </p>
    Type Minutes:
    <input class="mins" />
    <br>
    <br>
    <button onclick="GFG_Fun()">
        click here
    </button>
    <p id="GFG_DOWN">
    </p>
    <script>
        var up = document.getElementById('GFG_UP');
        var element = document.getElementById("body");
        up.innerHTML =
        "Click on the button to get minutes in Hours/Minutes format.";
         
        function conversion(mins) {
            // getting the hours.
            let hrs = Math.floor(mins / 60);
            // getting the minutes.
            let min = mins % 60;
            // formatting the hours.
            hrs = hrs < 10 ? '0' + hrs : hrs;
            // formatting the minutes.
            min = min < 10 ? '0' + min : min;
            // returning them as a string.
        return `${hrs}:${min}`;
        }
         
        function GFG_Fun() {
            var total = $('.mins').val();
            $('#GFG_DOWN').html(conversion(total));
        }
    </script>
</body>

Output:

Convert minutes to hours/minutes with the help of JQuery


Article Tags :