Add 10 seconds to a Date
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;
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
// let timeObject = new Date();
// let milliseconds= 10 * 1000; // 10 seconds = 10000 milliseconds
timeObject = new Date(timeObject.getTime() + milliseconds);
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
const timeObject = new Date();
timeObject = new Date(timeObject.getTime() + 1000 * 10);
console.log(timeObject);
Also please refer: How to add 30 minutes to a JavaScript Date object?
Try this
a = new Date();
a.setSeconds(a.getSeconds() + 10);
I have a couple of new variants
var t = new Date(Date.now() + 10000);
var t = new Date(+new Date() + 10000);
timeObject.setSeconds(timeObject.getSeconds() + 10)
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:
you can use
setSeconds
method by getting seconds from today and just adding 10 seconds in itvar today = new Date(); today.setSeconds(today.getSeconds() + 10);
You can add 10 *1000 milliseconds to the new date:
var today = new Date(); today = new Date(today.getTime() + 1000*10);
You can use
setTime
:today.setTime(now.getTime() + 10000)
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 );
}
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)
If you need to add 10 seconds to current time you can use:
const date = new Date();
date.setUTCSeconds(date.getUTCSeconds() + 10); // Change 10 to any number of seconds
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());
精彩评论