Coder Perfect

How do I add 30 minutes to a Date object in JavaScript?

Problem

I’m looking for a Date object that is 30 minutes behind another Date object. What’s the best way to do it with JavaScript?

Asked by Morgan Cheng

Solution #1

If you work with dates frequently, consider using JavaScript date libraries such as Datejs or Moment.js. With Moment.js, for example, it’s as simple as:

var newDateObj = moment(oldDateObj).add(30, 'm').toDate();

This is a one-line version of chaos’s response:

var newDateObj = new Date(oldDateObj.getTime() + diff*60000);

Where diff is the number of minutes you wish to subtract from oldDateObj’s current time. It can even be detrimental.

If you need to do this in several locations, you may save it as a reusable function:

function addMinutes(date, minutes) {
    return new Date(date.getTime() + minutes*60000);
}

And, in case it wasn’t clear, the reason we multiply minutes by 60000 is to convert them to milliseconds.

Isn’t it true that you may add 24 hours to a date to get the date for tomorrow? Wrong!

addMinutes(myDate, 60*24); //DO NOT DO THIS

It turns out that a day is not always 24 hours long if the user observes daylight saving time. There are two days in the year that are only 23 hours long and one that is 25 hours long. For example, in most of the United States and Canada, November 2, 2014, is still November 2, 2014, 24 hours after midnight.

const NOV = 10; //because JS months are off by one...
addMinutes(new Date(2014, NOV, 2), 60*24); //In USA, prints 11pm on Nov 2, not 12am Nov 3!

This is why, if you have a lot of work to do with this, using one of the aforementioned libraries is a safer bet.

I built a more generic version of this method, which you can see below. Although I still encourage using a library, it may be overkill or impractical for your project. The syntax is modeled after MySQL DATE_ADD function.

/**
 * Adds time to a date. Modelled after MySQL DATE_ADD function.
 * Example: dateAdd(new Date(), 'minute', 30)  //returns 30 minutes from now.
 * https://stackoverflow.com/a/1214753/18511
 * 
 * @param date  Date to start with
 * @param interval  One of: year, quarter, month, week, day, hour, minute, second
 * @param units  Number of units of the given interval to add.
 */
function dateAdd(date, interval, units) {
  if(!(date instanceof Date))
    return undefined;
  var ret = new Date(date); //don't change original date
  var checkRollover = function() { if(ret.getDate() != date.getDate()) ret.setDate(0);};
  switch(String(interval).toLowerCase()) {
    case 'year'   :  ret.setFullYear(ret.getFullYear() + units); checkRollover();  break;
    case 'quarter':  ret.setMonth(ret.getMonth() + 3*units); checkRollover();  break;
    case 'month'  :  ret.setMonth(ret.getMonth() + units); checkRollover();  break;
    case 'week'   :  ret.setDate(ret.getDate() + 7*units);  break;
    case 'day'    :  ret.setDate(ret.getDate() + units);  break;
    case 'hour'   :  ret.setTime(ret.getTime() + units*3600000);  break;
    case 'minute' :  ret.setTime(ret.getTime() + units*60000);  break;
    case 'second' :  ret.setTime(ret.getTime() + units*1000);  break;
    default       :  ret = undefined;  break;
  }
  return ret;
}

Working jsFiddle demo.

Answered by Kip

Solution #2

var d1 = new Date (),
    d2 = new Date ( d1 );
d2.setMinutes ( d1.getMinutes() + 30 );
alert ( d2 );

Answered by Jamie

Solution #3

Answered by chaos

Solution #4

Answered by Teo Graca

Solution #5

Maybe something along these lines?

Answered by Tyler Carter

Post is based on https://stackoverflow.com/questions/1197928/how-to-add-30-minutes-to-a-javascript-date-object