How to Add Minutes to a Date in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
4 min

banner

# Add Minutes to a Date in JavaScript

To add minutes to a date:

  1. Use the getMinutes() method to get the minutes of the specific date.
  2. Use the setMinutes() method to set the minutes for the date.
  3. The setMinutes method takes the minutes as a parameter and sets the value for the date.
index.js
function addMinutes(date, minutes) { date.setMinutes(date.getMinutes() + minutes); return date; } // โœ… Add 10 minutes to the current date const result1 = addMinutes(new Date(), 10); console.log(result1); // ๐Ÿ‘‰๏ธ 2023-07-27T17:24:52.897Z // ----------------------------------------------- // โœ… Add 20 minutes to a different date const date = new Date('2024-03-14T09:32:03.000Z'); const result2 = addMinutes(date, 20); console.log(result2); // ๐Ÿ‘‰๏ธ 2024-03-14T09:52:03.000Z

add minutes to date

The code for this article is available on GitHub

The addMinutes function takes a Date object and N as parameters and adds N minutes to the date.

If you need to add minutes to the current date, call the Date() constructor without passing it any arguments.

index.js
function addMinutes(date, minutes) { date.setMinutes(date.getMinutes() + minutes); return date; } // โœ… Add 10 minutes to the current date const currentDate = new Date(); const result1 = addMinutes(currentDate, 10); console.log(result1); // ๐Ÿ‘‰๏ธ 2023-07-27T17:26:05.616Z

add minutes to current date

The Date.getMinutes() method returns a number between 0 and 59 that represents the minutes in the given date.

index.js
console.log(new Date().getMinutes()); // ๐Ÿ‘‰๏ธ 36

The setMinutes() method takes a number representing the minutes as a parameter and sets the value on the date.

The JavaScript Date object automatically takes care of rolling over the hours, days, months and years if adding X minutes to the date changes their values.

index.js
function addMinutes(date, minutes) { date.setMinutes(date.getMinutes() + minutes); return date; } const date = new Date('2024-03-14T09:32:03.000Z'); const result = addMinutes(date, 36); console.log(result); // ๐Ÿ‘‰๏ธ 2024-03-14T10:08:03.000Z

We added 36 minutes to the date, so the hour had to be adjusted.

The Date object would automatically take care of adjusting the day of the month, the month and the year as well.

# Add minutes to Date without mutation

Note that the setMinutes method mutates the Date object it was called on.

If you don't want to change the Date in place, you can create a copy of it before calling the method.

index.js
function addMinutes(date, minutes) { const dateCopy = new Date(date.getTime()); dateCopy.setMinutes(dateCopy.getMinutes() + minutes); return dateCopy; } const date = new Date('2024-03-14T09:32:03.000Z'); const result = addMinutes(date, 20); console.log(result); // ๐Ÿ‘‰๏ธ 2024-03-14T09:52:03.000Z console.log(date); // ๐Ÿ‘‰๏ธ 2024-03-14T09:32:03.000Z

add minutes to date without mutation

The code for this article is available on GitHub

When a Date object is passed to the Date() constructor, it gets converted to a timestamp and can be used to create a copy of the date.

Mutating function parameters is a bad practice because calling the function with the same parameter multiple times returns different results.

Instead, pure functions like the one above return the same output when called with the same parameters.

# Add Minutes to a Date using date-fns

You can also use date-fns module to add minutes to a date.

index.js
import {addMinutes} from 'date-fns'; const date = new Date('2024-03-14T09:32:03.000Z'); const result1 = addMinutes(date, 20); console.log(result1); // ๐Ÿ‘‰๏ธ 2024-03-14T09:52:03.000Z const result2 = addMinutes(date, 32); console.log(result2); // ๐Ÿ‘‰๏ธ 2024-03-14T10:04:03.000Z console.log(date); // ๐Ÿ‘‰๏ธ 2024-03-14T09:32:03.000Z

add minutes to date using date fns

The code for this article is available on GitHub

The addMinutes() function takes a date and the number of minutes to be added to the date as parameters.

The function doesn't mutate the original date as shown in the example.

If you don't have date-fns installed, you can install it by running the following command from your terminal.

shell
# ๐Ÿ‘‡๏ธ create package.json if you don't have one npm init -y # โœ… install with NPM npm install date-fns # โœ… install with YARN yarn add date-fns

# Add Minutes to a Date using moment.js

You can also use the moment.js module to add minutes to a date.

index.js
import moment from 'moment'; const date = new Date('2024-03-14T09:32:03.000Z'); const result1 = moment(date).add(20, 'minutes'); console.log(result1); // ๐Ÿ‘‰๏ธ 2024-03-14T09:52:03.000Z const result2 = moment(date).add(31, 'minutes'); console.log(result2); // ๐Ÿ‘‰๏ธ 2024-03-14T10:04:03.000Z console.log(date); // ๐Ÿ‘‰๏ธ 2024-03-14T09:32:03.000Z

add minutes to date using moment js

The code for this article is available on GitHub

We used the moment().add() method to add minutes to a date.

The method can be used to add years, quarters, months, weeks, days, hours, minutes, seconds or milliseconds to a date.

If you don't have moment installed, you can install it by running the following command from your terminal.

shell
# ๐Ÿ‘‡๏ธ create package.json if you don't have one npm init -y # โœ… install with NPM npm install moment # โœ… install with YARN yarn add moment

The call to the add() method actually returns a moment object and not a native JavaScript date.

If you need to convert the value to a JavaScript date, use the toDate() method.

index.js
import moment from 'moment'; const date = new Date('2024-03-14T09:32:03.000Z'); const result1 = moment(date).add(20, 'minutes').toDate(); console.log(result1); // ๐Ÿ‘‰๏ธ 2024-03-14T09:52:03.000Z const result2 = moment(date).add(31, 'minutes').toDate(); console.log(result2); // ๐Ÿ‘‰๏ธ 2024-03-14T10:04:03.000Z console.log(date); // ๐Ÿ‘‰๏ธ 2024-03-14T09:32:03.000Z
The code for this article is available on GitHub

The toDate() method takes care of converting the moment object to a native JavaScript Date object.

I've also written an article on how to subtract hours or minutes from a Date.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev