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

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

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

Approach-1

<!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>

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);

Ashok Patel

I'm an electronic engineer working in a multinational company,having good experience on Electronics and electrical engineers design and oversee production of electronic equipment such as radios, televisions, computers, washing machines and telecommunication systems.I like to do RND and Research.I also have hands on experience graphic design software and in web designing having great command on ASP.NET, HTML5, JavaScript, T-SQL, JQuery.

Add comment

Donate for Corona Victim

Corona Virus Relief Fund

Your Header Sidebar area is currently empty. Hurry up and add some widgets.