[Solved]-How to add minutes in Date Object using javascript


As we know, JavaScript's Date object does not have any additional or subtract function for performing calculations between two date objects. Therefore, I have simply written a function that can do this:

Approach-1


Date.prototype.addHours = function (h) { ... }: This line extends the Date object's prototype, allowing us to add a new method called addHours. This method takes one parameter h, which represents the number of hours to add to the date.

this.setTime(this.getTime() + (h * 60 * 60 * 1000));: Inside the addHours method, this line modifies the current date's time value. It calculates the number of milliseconds represented by the specified number of hours (h * 60 * 60 * 1000) and adds this value to the current date's time using the setTime method.

return this;: After modifying the date's time value, the addHours method returns the modified date object.

document.write(new Date().addHours(4));: This line tests the addHours function by creating a new Date object representing the current date and time (new Date()), and then calling the addHours method with an argument of 4 to add 4 hours to the current date. Finally, it writes the modified date to the document using document.write.
<!DOCTYPE html>
<html>
<head>
    <title>Adding hours-JavaScript</title>
</head>
<body>
    <script>
        Date.prototype.addHours = function (h) {
            this.setTime(this.getTime() + (h * 60 * 60 * 1000));
            return this;
        }
        //to test this function c
        document.write(new Date().addHours(4));
    </script>
</body>
</html>

Approach-2

Using .getMinutes() to get the current minutes of Date Object ,then add minutes and use .setMinutes() to update the current  date object.
<!DOCTYPE html>
<html>
<head>
    <title>Adding hours-JavaScript</title>
</head>
<body>
    <script>
        
        var currentdate = new Date();
        currentdate.setMinutes(currentdate.getMinutes() + 20);
        alert(currentdate);
    </script>
</body>
</html>

Instantiating a Date Object:

var currentdate = new Date();: This line creates a new Date object named currentdate, representing the current date and time when the script is executed.

Adding Minutes:

currentdate.setMinutes(currentdate.getMinutes() + 20);: Here, the setMinutes() method is used to add 20 minutes to the current time stored in the currentdate object. It retrieves the current minutes using currentdate.getMinutes(), adds 20 to it, and then updates the minutes value of the currentdate object.

Displaying the Result:

alert(currentdate);: This line displays an alert dialog containing the updated value of currentdate, which now represents the current date and time 20 minutes ahead of the original time.

Now we can easily able Add hours to JavaScript Date object using above two function.Let take some exmaple.

  • How to add 20 minutes to a current date?
 var currentdate = new Date();
        currentdate.setMinutes(currentdate.getMinutes() + 20);
        alert(currentdate);

 

  • Add 5 minutes to current time javascript

 

 var currentdate = new Date();
        currentdate.setMinutes(currentdate.getMinutes() + 5);
        alert(currentdate);
  • How to add 30 minutes to a JavaScript Date object?
var currentdate = new Date();
        currentdate.setMinutes(currentdate.getMinutes() + 30);
        alert(currentdate);