The setMinutes()
and getMinutes()
methods of the Date
object can be used to increment or decrement minutes from a date in JavaScript.
The following example demonstrates how you can add 15 minutes to the current date:
const today = new Date()
const nextDate = new Date()
// Add 15 Minutes
nextDate.setMinutes(today.getMinutes() + 15)
You can also update the existing JavaScript Date
object as shown below:
const date = new Date()
date.setMinutes(date.getMinutes() + 15)
To subtract minutes from a date in JavaScript, simply minus the number of minutes when updating the date with the setMinutes()
method:
const today = new Date()
const lastDate = new Date()
// Minus 30 Minutes
lastDate.setMinutes(today.getMinutes() - 15)
Finally, if you are frequently manipulating dates in JavaScript, add a function to Date
’s prototype and call it directly whenever you want to add or subtract minutes:
Date.prototype.addMinutes = function (mins) {
const date = new Date(this.valueOf())
date.setMinutes(date.getMinutes() + mins)
return date
}
// Add 45 Minutes
const nextMinutes = new Date(2022, 10, 15)
console.log(nextMinutes.addMinutes(45))
// Tue Nov 15 2022 00:45:00 GMT+0500 (Pakistan Standard Time)
// Minus 30 Minutes
const lastMinutes = new Date(2022, 10, 15)
console.log(lastMinutes.addMinutes(-30))
// Mon Nov 14 2022 23:30:00 GMT+0500 (Pakistan Standard Time)