JavaScript Add Minutes to Date


When working with time-related functionalities in JavaScript, manipulating dates by adding or subtracting specific time units, such as minutes, is a frequent requirement.

Adding minutes to a Date object is essential in scenarios like countdown timers, event scheduling, or any application dealing with time-sensitive operations.

    Table of Contents

  1. Using setMinutes() Method
  2. Using getTime() and setTime() Methods
  3. Using the Date Constructor
  4. Using Libraries like date-fns or moment.js
  5. Conclusion

1. Using setMinutes() Method

The setMinutes() method of the Date object allows us to set the minutes of a date by providing a specific value.

The idea is to get the current minutes of the date and add the desired minutes to it. If the value exceeds 60, the date object will automatically adjust the hours and days accordingly.

Example

let date = new Date();
console.log(date);

// 👉️ adding 10 minutes to the current date
date.setMinutes(date.getMinutes() + 10);
console.log(date);

2. Using getTime() and setTime() Methods

Another way to add time to a date is to convert the date to milliseconds using getTime(), add the required minutes in milliseconds, and then set the updated time using setTime().

A minute is equal to 60,000 milliseconds (60 * 1000). So, we can add the required minutes in milliseconds and then set the updated time using setTime().

Example

let date = new Date();
console.log(date);

// 👉️ adding 10 minutes to the current date
date.setTime(date.getTime() + (10 * 60 * 1000));
console.log(date);

3. Using the Date Constructor

The Date constructor can also be used to add minutes to a date. For this you can pass time in milliseconds and add the required minutes in milliseconds to it.

Example

let date = new Date();
console.log(date);

// 👉️ adding 10 minutes to the current date
date = new Date(date.getTime() + (10 * 60 * 1000));
console.log(date);

4. Using Libraries like date-fns or moment.js

There are many third-party libraries that provides direct methods to manipulate minutes in a date object.

Using date-fns

The date-fns has a method called addMinutes() that can be used to add minutes to a date object.

var { addMinutes } = require('date-fns');

let date = new Date();
console.log(date);

// 👉️ adding 10 minutes to the current date
date = addMinutes(date, 10);
console.log(date);

Using moment.js

The moment.js has a method called add() it takes 2 parameters, the first one is the unit of time and the second one is the value to be added.

Here we will pass minutes as the first parameter and 10 as the second parameter.

let moment = require('moment');

let date = new Date();
console.log(date);

// 👉️ adding 10 minutes to the current date
date = moment(date).add('minutes', 10).toDate();
console.log(date);

Conclusion

Adding minutes to a JavaScript Date object presents various approaches, each with its advantages. Whether employing native methods such as setMinutes() or getTime() with setTime(), or harnessing the capabilities of third-party libraries like date-fns or moment.js.