JavaScript

Add Minutes to a Date in JavaScript

While programming in JavaScript, there can be a requirement to display the date of multiple regions automatically. For instance, the option for changing the region manually from time to time is not provided while dealing with code. To achieve this, adding minutes to date in JavaScript is of great aid in setting the minutes with respect to various regions.

This blog will explain the procedure of adding minutes to date in JavaScript.

How to Add Minutes to a Date in JavaScript?

To add minutes to date in JavaScript, apply the following approaches:

  • setMinutes()” and “getMinutes()” methods.
  • User-defined” function.
  • getTime()” method.

Approach 1: Add Minutes to a Date in JavaScript Using setMinutes() and getMinutes() Methods

The “setMinutes()” method sets the date minutes, and the “getMinutes()” method gives the minutes from 0 to 59 in a date. These methods can be applied to set the date such that a particular set of minutes become added to the fetched minutes in the date.

Syntax

Date.setMinutes(min, sec, millisec)

In the above syntax:

min”, “sec”, and “millisec” refer to the set time format.

Date.getMinutes()

In the given syntax:

The current minutes with respect to the will be fetched.

Example
Let’s overview the following example:

<script type="text/javascript">
let currentDate = new Date();
console.log("The current date and time is:",currentDate)
currentDate.setMinutes(currentDate.getMinutes() + 25);
console.log("The new date and time after the added minutes become:", currentDate);
</script>

Apply the below-given steps as stated in the code snippet:

  • Firstly, specify the “new” keyword and apply the “Date()” constructor side by side to fetch the current date and time and display it.
  • After that, apply the “setMinutes()” method to set the minutes such that “25” minutes will be added to the fetched minutes via the “getMinutes()” method in the current date.
  • Lastly, display the date and time with added minutes in it.

Output

In the above output, it can be visualized that the stated minutes are added to the current time.

Approach 2: Add Minutes to a Date in JavaScript Using User-Defined Function

This approach can be implemented to add the passed minutes in the current date upon invoking the function.

Example
The below-given example illustrates the stated concept:

<script type="text/javascript">
function addMinutes(date, minutes) {
 date.setMinutes(date.getMinutes() + minutes);
 return date;
}
let currentDate = new Date();
console.log("The current date and time is:", currentDate)
let updateDate = addMinutes(currentDate, 10);
console.log("The new date and time after the added minutes become:", updateDate);
</script>

In the above lines of code:

  • Define a function named “addMinutes()” having the stated parameters.
  • In its definition, apply the “setMinutes()” and “getMinutes()” methods in combination.
  • The stated methods will work in such a way that the passed minutes as arguments will be added to the current date.
  • After that, likewise, fetch the current date and time via the “Date” constructor and display it.
  • Finally, invoke the defined function by passing the fetched date in the previous step and the stated minutes, respectively, as arguments.
  • This will add “10” minutes to the fetched current date.

Output

From the above output, the time difference of “10” minutes in both statements can be observed.

Approach 3: Add Minutes to a Date in JavaScript Using getTime() Method

The “getTime()” method gives the number of milliseconds elapsed since January 1, 1970. This method can be utilized to add the user-defined minutes to the current date.

Example
Let’s go through the below-stated example:

<script type="text/javascript">
let addMinutes = prompt("Enter the minutes to add")
let currentDate = new Date();
console.log("The current date and time is:",currentDate)
let updateDate = new Date(currentDate.getTime() + addMinutes * 60000)
console.log("The new date and time after the added minutes become:",updateDate)
</script>

Perform the following steps as given in the above code:

  • Firstly, ask the user to enter the minutes to be added.
  • In the next step, fetch the current date similarly via the “Date()” constructor and display it.
  • After that, apply the “getTime()” method by referring to the fetched date in the previous step. This will extract the current time from the date.
  • Also, access the user-defined minutes and multiply them such that the entered minutes become added properly.
  • Note: Algorithm(x * 60000 => 20 * 60000=>1200000 milliseconds = [20 minutes].
  • In the above algorithm, “x” represents the user-defined number.
  • Lastly, add the user input minutes to the extracted time from the date, which will resultantly add the minutes to the current date.

Output

The above output signifies the time difference of “20” minutes in both statements.

Conclusion

The “setMinutes()” and “getMinutes()” methods, the “user-defined” function, or the “getTime()” method can be applied to add minutes to date in JavaScript. The setMinutes() and getMinutes() methods can be utilized to get the minutes from the date and add the particular minutes to them. The user-defined function can be implemented to add the passed minutes as the function’s argument to the date. Whereas the getTime() method can be applied by taking the user input minutes and appending them to the current date. This blog explained how to add minutes to date in JavaScript.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.