Javascript 为日期添加 10 秒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7687884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add 10 seconds to a Date
提问by george
How can I add 10 seconds to a JavaScript date object?
如何向 JavaScript 日期对象添加 10 秒?
Something like this:
像这样的东西:
var timeObject = new Date()
var seconds = timeObject.getSeconds() + 10;
timeObject = timeObject + seconds;
回答by zzzzBov
There's a setSeconds
method as well:
还有一个setSeconds
方法:
var t = new Date();
t.setSeconds(t.getSeconds() + 10);
For a list of the otherDate
functions, you should check out MDN
有关其他Date
功能的列表,您应该查看 MDN
setSeconds
will correctly handle wrap-around cases:
setSeconds
将正确处理环绕情况:
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
回答by 4esn0k
// let timeObject = new Date();
// let seconds = 10 * 1000; // 10000 milliseconds
timeObject = new Date(timeObject.getTime() + seconds);
回答by Danpe
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
5,196,949 次操作/秒,最快
setSeconds
设置秒数
var d = new Date('2014-01-01 10:11:55');
d.setSeconds(d.getSeconds() + 10);
2,936,604 Ops/sec, 43% slower
2,936,604 次操作/秒,慢 43%
moment.js
时刻.js
var d = new moment('2014-01-01 10:11:55');
d = d.add(10, 'seconds');
22,549 Ops/sec, 100% slower
22,549 次操作/秒,慢 100%
So maybe its the least human readable (not that bad) but the fastest way of going :)
所以也许它是人类最不可读的(不是那么糟糕),但它是最快的方式:)
回答by Ron
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?
回答by lostyzd
Try this
尝试这个
a = new Date();
a.setSeconds(a.getSeconds() + 10);
回答by Tomasz Nurkiewicz
timeObject.setSeconds(timeObject.getSeconds() + 10)
回答by 1nstinct
I have a couple of new variants
我有几个新的变体
var t = new Date(Date.now() + 10000);
var t = new Date(+new Date() + 10000);
var t = new Date(Date.now() + 10000);
var t = new Date(+new Date() + 10000);
回答by coding_is_fun
The Date()
object in javascript is not that smart really.
Date()
javascript 中的对象并不是那么聪明。
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).
如果您只专注于增加秒数,它似乎可以顺利处理事情,但是如果您尝试添加 X 秒数,然后将 X 分钟数和小时数等添加到同一个Date
对象上,您最终会遇到麻烦。所以我只是回到只使用该setSeconds()
方法并将我的数据转换为秒(效果很好)。
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.
如果有人可以演示Date()
使用所有 set 方法向全局对象添加时间并正确显示最终时间,我希望看到它,但我感觉一次将在给定Date()
对象上使用一个 set 方法,并且混合它们会导致混乱。
var vTime = new Date();
var iSecondsToAdd = ( iSeconds + (iMinutes * 60) + (iHours * 3600) + (iDays * 86400) );
vTime.setSeconds(iSecondsToAdd);
回答by Javed IN
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)
您可以
setSeconds
通过从今天开始获取秒数并在其中添加 10 秒来使用方法var today = new Date(); today.setSeconds(today.getSeconds() + 10);
您可以向新日期添加 10 *1000 毫秒:
var today = new Date(); today = new Date(today.getTime() + 1000*10);
您可以使用
setTime
:today.setTime(now.getTime() + 10000)
回答by Ikram - Ud - Daula
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)
只需调用并分配 new Date().addSeconds(10)