How to add minutes to date in JavaScript date object

By Saruque Ahamed Mollick

In this JavaScript tutorial, I will show you how to add minutes to date in JavaScript. That means adding minutes to any JavaScript date object. I hope this is going to be very much useful. Here we are going to use to JavaScript methods

  • getMinutes()
  • setMinutes()

how to subtract days from a date in javascript

How to compare two dates in JavaScript

Add minutes to date in JavaScript date object

In the below example we add 5 minutes to the current time using JavaScript

<!DOCTYPE html>
<html>
<head>
  <title>Title Goes Here</title>
</head>
<body>
  <script type="text/javascript">
     var date1 = new Date();
     date1.setMinutes(date1.getMinutes()+5);
     document.write(date1);
  </script>
</body>
</html>

Output:
Thu Oct 18 2018 19:47:03 GMT+0530 (India Standard Time)

 

The current time in my system was 19:42:03 but this JavaScript program print the current time by adding 5 minutes.



date1.setMinutes(date1.getMinutes()+n);

You can change the value of n. here n will be the time in minutes you want to be added with the current time.

 var date1 = new Date();

here you can also pass the date and time as per your choice.

getMinutes() method

getMinutes() method returns the time in minutes ( local time )

var mytime = new Date();
var show = mytime.getMinutes();
document.write(show);

It will show you the current minute.

Now, to set the time as per our choice or to modify the time. Like if you want to subtract minutes to the current time or add some minutes to the current time you can use setMinutes() method.

setMinutes() method

setMinutes() method sets the time in minutes ( local time ) but this method can also be used to set seconds and milliseconds too.

var mytime = new Date();
var show = mytime.setMinutes(48);
document.write(show);

It will show you the time but the minute will be set to 48 minutes.

i.e, if the time is 19:45:23 it will show you 19:48:23
If you have any doubts feel free to comment below.

how to subtract days from a date in javascript

Leave a Reply

Your email address will not be published. Required fields are marked *