给日期增加10秒 [英] Add 10 seconds to a Date
本文介绍了给日期增加10秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何向JavaScript日期对象添加10秒?
How can I add 10 seconds to a JavaScript date object?
类似这样的东西:
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
将正确处理环绕情况:
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
这篇关于给日期增加10秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文