Skip to main content
update linked libraries, and use https links
Source Link
Kip
  • 108.7k
  • 87
  • 234
  • 265

If you are doing a lot of date work, you may want to look into JavaScript date libraries like DatejsLuxon, Day.js, or Moment.jsMoment.js. For example, with Moment.js, this is simply:

Below is a more generic version of this function that I wrote. I'd still recommend using a library, but that may be overkill/impossible for your project. The syntax is modeled after MySQL DATE_ADDMySQL DATE_ADD function.

Working jsFiddle demoWorking jsFiddle demo.

If you are doing a lot of date work, you may want to look into JavaScript date libraries like Datejs or Moment.js. For example, with Moment.js, this is simply:

Below is a more generic version of this function that I wrote. I'd still recommend using a library, but that may be overkill/impossible for your project. The syntax is modeled after MySQL DATE_ADD function.

Working jsFiddle demo.

If you are doing a lot of date work, you may want to look into JavaScript date libraries like Luxon, Day.js, or Moment.js. For example, with Moment.js, this is simply:

Below is a more generic version of this function that I wrote. I'd still recommend using a library, but that may be overkill/impossible for your project. The syntax is modeled after MySQL DATE_ADD function.

Working jsFiddle demo.

updates for 10-year anniversary of this post
Source Link
Kip
  • 108.7k
  • 87
  • 234
  • 265

And just in case this is not obvious, the reason we multiply minutes by 60000 is to convert minutes to milliseconds.

const NOV = 10; //because JS months are off by one...
addMinutes(new Date(2014, 10NOV, 2), 60*24); //In USA, prints 11pm on Nov 2, not 12am Nov 3!
/**
 * 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 demoWorking jsFiddle demo.

addMinutes(new Date(2014, 10, 2), 60*24); //In USA, prints 11pm on Nov 2, not 12am Nov 3!
/**
 * 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) {
  var ret = new Date(date); //don't change original date
  var checkRollover = function() { if(ret.getDate() != date.getDate()) ret.setDate(0);};
  switch(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.

And just in case this is not obvious, the reason we multiply minutes by 60000 is to convert minutes to milliseconds.

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!
/**
 * 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.

fix long-standing bug...
Source Link
Kip
  • 108.7k
  • 87
  • 234
  • 265
addMinutes(new Date('2014-11-02'2014, 10, 2), 60*24); //In USA, prints 11pm on Nov 2, not 12am Nov 3!
addMinutes(new Date('2014-11-02'), 60*24); //In USA, prints 11pm on Nov 2, not 12am Nov 3!
addMinutes(new Date(2014, 10, 2), 60*24); //In USA, prints 11pm on Nov 2, not 12am Nov 3!
update moment example
Source Link
Kip
  • 108.7k
  • 87
  • 234
  • 265
Loading
Add url to this answer for copy/pasters
Source Link
Kip
  • 108.7k
  • 87
  • 234
  • 265
Loading
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot
Loading
typo
Source Link
Kip
  • 108.7k
  • 87
  • 234
  • 265
Loading
bug fixes and performance improvements
Source Link
Kip
  • 108.7k
  • 87
  • 234
  • 265
Loading
easier-to-read example
Source Link
Kip
  • 108.7k
  • 87
  • 234
  • 265
Loading
edited body
Source Link
Kip
  • 108.7k
  • 87
  • 234
  • 265
Loading
updated code to add hours/minutes/seconds
Source Link
Kip
  • 108.7k
  • 87
  • 234
  • 265
Loading
more improvement!
Source Link
Kip
  • 108.7k
  • 87
  • 234
  • 265
Loading
before someone nitpicks...
Source Link
Kip
  • 108.7k
  • 87
  • 234
  • 265
Loading
deleted 6 characters in body
Source Link
Kip
  • 108.7k
  • 87
  • 234
  • 265
Loading
added 658 characters in body
Source Link
Kip
  • 108.7k
  • 87
  • 234
  • 265
Loading
improving answer
Source Link
Kip
  • 108.7k
  • 87
  • 234
  • 265
Loading
Source Link
Kip
  • 108.7k
  • 87
  • 234
  • 265
Loading