Javascript 如何将当前日期添加 20 分钟?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4517672/
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
How to add 20 minutes to a current date?
提问by Richard Knop
Possible Duplicate:
How to add 30 minutes to a javascript Date object?
I can get the current date object like this:
我可以像这样获取当前日期对象:
var currentDate = new Date();
How can I add 20 minutes to it?
我怎样才能增加 20 分钟呢?
var twentyMinutesLater = ?;
回答by Nick Craver
Use .getMinutes()
to get the current minutes, then add 20 and use .setMinutes()
to update the date object.
使用.getMinutes()
得到当前分钟,然后加入20和使用.setMinutes()
更新日期对象。
var twentyMinutesLater = new Date();
twentyMinutesLater.setMinutes(twentyMinutesLater.getMinutes() + 20);
回答by T.J. Crowder
Add it in milliseconds:
以毫秒为单位添加它:
var currentDate = new Date();
var twentyMinutesLater = new Date(currentDate.getTime() + (20 * 60 * 1000));
回答by e2-e4
Just add 20 minutes in milliseconds to your date:
只需在您的日期中添加 20 分钟(以毫秒为单位):
var currentDate = new Date();
currentDate.setTime(currentDate.getTime() + 20*60*1000);
回答by Michael Borgwardt
Just get the millisecond timestamp and add 20 minutes to it:
只需获取毫秒时间戳并为其添加 20 分钟:
twentyMinutesLater = new Date(currentDate.getTime() + (20*60*1000))
回答by pooja
var d = new Date();
var v = new Date();
v.setMinutes(d.getMinutes()+20);