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

I want a Date object that is 30 minutes later than another Date object. How do I use JavaScript?

#1st floor

var oldDateObj = new Date(); var newDateObj = new Date(); newDateObj.setTime(oldDateObj.getTime() + (30 * 60 * 1000)); console.log(newDateObj);

#2nd floor

Maybe so?

var d = new Date(); var v = new Date(); v.setMinutes(d.getMinutes()+30); console.log(v)

#3rd floor

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

#4th floor

Using the library

If you are doing a lot of date work, you may need to study such as Datejs or Moment.js Class. For example, using Moment.js is simple:

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

Vanilla Javascript

It's like A confused answer , but in one line:

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

diff is the number of minutes you want to differ from the oldDateObj time. It could even be negative.

Or as a reusable feature, if you need to do so in multiple locations:

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

In case this is not obvious, the reason we multiply minutes by 60000 is to convert minutes into milliseconds.

Be careful with vanilla Javascript. Dates are hard!

You may think you can add 24 hours to the date to get tomorrow's date, right? Wrong!

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

It turns out that a day is not necessarily 24 hours if the user follows daylight saving time. One day of the year is only 23 hours long, and one day of the year is only 25 hours long. For example, in most parts of the United States and Canada, 24 midnight on November 2, 2014 is still November 2:

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!

That's why using one of these libraries is a safer option if a lot of work has to be done to do it.

Here is a more general version of this function that I wrote. I still recommend that you use a library, but this may be redundant / impossible for your project. Grammar is in 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.
 * @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 .

#5th floor

Another option for me to write:

DP? Dateextensions Library

If this is all the date processing you need, it's too much, but it will do what you want.

Support date / time format, date Mathematics (plus / minus date part), date comparison, date analysis, etc. It's open source.

Keywords: Javascript MySQL

Added by ruttegar on Mon, 23 Dec 2019 01:05:00 +0200