Add 10 seconds to a Date

Javascript

Javascript Problem Overview


How can I add 10 seconds to a JavaScript date object?

Something like this:

var timeObject = new Date()		
var seconds = timeObject.getSeconds() + 10;
timeObject = timeObject + seconds;

Javascript Solutions


Solution 1 - Javascript

There's a setSeconds method as well:

var t = new Date();
t.setSeconds(t.getSeconds() + 10);

For a list of the other Date functions, you should check out MDN


setSeconds will correctly handle wrap-around cases:

var d;
d = new Date('2014-01-01 10:11:55');
alert(d.getMinutes() + ':' + d.getSeconds()); //11:55
d.setSeconds(d.getSeconds() + 10);
alert(d.getMinutes() + ':0' + d.getSeconds()); //12:05

Solution 2 - Javascript

// let timeObject = new Date();
// let milliseconds= 10 * 1000; // 10 seconds = 10000 milliseconds
timeObject = new Date(timeObject.getTime() + milliseconds);

Solution 3 - Javascript

Just for the performance maniacs among us.

getTime

var d = new Date('2014-01-01 10:11:55');
d = new Date(d.getTime() + 10000);

5,196,949 Ops/sec, fastest


setSeconds

var d = new Date('2014-01-01 10:11:55');
d.setSeconds(d.getSeconds() + 10);

2,936,604 Ops/sec, 43% slower


moment.js

var d = new moment('2014-01-01 10:11:55');
d = d.add(10, 'seconds');

22,549 Ops/sec, 100% slower


So maybe its the least human readable (not that bad) but the fastest way of going :)

jspref online tests

Solution 4 - Javascript

const timeObject = new Date(); 
timeObject = new Date(timeObject.getTime() + 1000 * 10);
console.log(timeObject);

Also please refer: https://stackoverflow.com/questions/1197928/how-to-add-30-minutes-to-a-javascript-date-object

Solution 5 - Javascript

Try this

a = new Date();
a.setSeconds(a.getSeconds() + 10);

Solution 6 - Javascript

timeObject.setSeconds(timeObject.getSeconds() + 10)

Solution 7 - Javascript

I have a couple of new variants

  1. var t = new Date(Date.now() + 10000);
  2. var t = new Date(+new Date() + 10000);

Solution 8 - Javascript

The Date() object in javascript is not that smart really.

If you just focus on adding seconds it seems to handle things smoothly but if you try to add X number of seconds then add X number of minute and hours, etc, to the same Date object you end up in trouble. So I simply fell back to only using the setSeconds() method and converting my data into seconds (which worked fine).

If anyone can demonstrate adding time to a global Date() object using all the set methods and have the final time come out correctly I would like to see it but I get the sense that one set method is to be used at a time on a given Date() object and mixing them leads to a mess.

var vTime = new Date();

var iSecondsToAdd = ( iSeconds + (iMinutes * 60) + (iHours * 3600) + (iDays * 86400) );

vTime.setSeconds(iSecondsToAdd);

Here is some more documentation that may help:

Solution 9 - Javascript

  1. you can use setSeconds method by getting seconds from today and just adding 10 seconds in it

    var today = new Date();
    today.setSeconds(today.getSeconds() + 10);
    
  2. You can add 10 *1000 milliseconds to the new date:

    var today = new Date(); 
    today = new Date(today.getTime() + 1000*10);
    
  3. You can use setTime:

    today.setTime(now.getTime() + 10000)
    

Solution 10 - Javascript

Try this way.

Date.prototype.addSeconds = function(seconds) {
  var copiedDate = new Date(this.getTime());
  return new Date(copiedDate.getTime() + seconds * 1000);
}

Just call and assign new Date().addSeconds(10)

Solution 11 - Javascript

The .setSeconds revealed quite strange misbehavior for me under node.js, so I use:

dateAddSeconds(date, seconds){ 
	return new Date( Date.parse(date) + seconds*1000 );
}

Solution 12 - Javascript

Few years ago i've wrote a 'generic' date function:

function addToDate({time_unit, operator, offset_value }) {
        const date = new Date();
  operator = operator == "after" ? "+" : "-";
  switch (time_unit) {
    case "seconds":
        date.setSeconds(eval(`${date.getSeconds()} ${operator} ${offset_value}`));
    break;

    case "hours":
        date.setHours(eval(`${date.getHours()} ${operator} ${offset_value}`));
      break;

    case "minutes":
        date.setMinutes(eval(`${date.getMinutes()} ${operator} ${offset_value}`));

      break;
    case "days":
        date.setDate(eval(`${date.getDate()} ${operator} ${offset_value}`));
      break;

    case "months":
        date.setMonth(eval(`${date.getMonth()} ${operator} ${offset_value}`));

      break;
    case "years":
        date.setFullYear(eval(`${date.getFullYear()} ${operator} ${offset_value}`));
      break;
    default:
      break;
  }

  return date;
}

const new_date = addToDate({time_unit:'seconds','operator':'after','offset_value':10});

console.log(new_date.toISOString());

Solution 13 - Javascript

If you need to add 10 seconds to current time you can use:

const date = new Date();
date.setUTCSeconds(10); // Change 10 to any number of seconds

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongeorgeView Question on Stackoverflow
Solution 1 - JavascriptzzzzBovView Answer on Stackoverflow
Solution 2 - Javascript4esn0kView Answer on Stackoverflow
Solution 3 - JavascriptDanpeView Answer on Stackoverflow
Solution 4 - JavascriptRonView Answer on Stackoverflow
Solution 5 - JavascriptlostyzdView Answer on Stackoverflow
Solution 6 - JavascriptTomasz NurkiewiczView Answer on Stackoverflow
Solution 7 - Javascript1nstinctView Answer on Stackoverflow
Solution 8 - Javascriptcoding_is_funView Answer on Stackoverflow
Solution 9 - JavascriptJaved INView Answer on Stackoverflow
Solution 10 - JavascriptIkram - Ud - DaulaView Answer on Stackoverflow
Solution 11 - JavascriptT.ToduaView Answer on Stackoverflow
Solution 12 - JavascriptEran PeledView Answer on Stackoverflow
Solution 13 - JavascriptAnasSafiView Answer on Stackoverflow