我想获得一个日期对象,它比另一个日期对象晚30分钟。我如何用JavaScript做到这一点?
当前回答
“添加”30分钟的一种方法是创建第二个日期对象(主要用于演示),并将分钟设置为分钟+ 30。如果第一次距离下一个小时不到30分钟,这也可以考虑调整时间。(即4:45至5:15)
const first = new Date(); console.log("第一次约会:",first. tostring ()); const second =新的日期(第一个); const newMinutes = second.getMinutes() + 30; console.log("new minutes:", newMinutes); second.setMinutes (newMinutes); console.log("second date:", second. tostring ());
其他回答
我知道这个话题太老了。但是我非常确定有一些开发人员仍然需要这个,所以我为您制作了这个简单的脚本。 希望你喜欢!
你好,现在是2020年,我做了一些修改,希望现在能更好地帮助你!
你好,现在是2022年,我又回来解决了一些问题,并为方法和函数提供了更好的命名。
function addTimeToDate(addedTime, date){ let generatedTime = date.getTime(); if(addedTime.seconds) generatedTime += 1000 * addedTime.seconds; //check for additional seconds if(addedTime.minutes) generatedTime += 1000* 60 * addedTime.minutes;//check for additional minutes if(addedTime.hours) generatedTime += 1000 * 60 * 60 * addedTime.hours;//check for additional hours return new Date(generatedTime); } Date.prototype.addTime = function(addedTime){ return addTimeToDate(addedTime, this); } let futureDate = new Date().addTime({ hours: 16, //Adding one hour minutes: 45, //Adding fourty five minutes seconds: 0 //Adding 0 seconds return to not adding any second so we can remove it. }); <button onclick="console.log(futureDate)">Travel to the future</button>
这就是我所做的,似乎很有效:
Date.prototype.addMinutes = function(minutes) {
var copiedDate = new Date(this.getTime());
return new Date(copiedDate.getTime() + minutes * 60000);
}
然后你可以这样调用它:
var now = new Date();
console.log(now.addMinutes(50));
我总是创建7个函数,在JS中使用date: addSeconds, addMinutes, addHours, addDays, addWeeks, addMonths, addYears。
你可以在这里看到一个例子:http://jsfiddle.net/tiagoajacobi/YHA8x/
使用方法:
var now = new Date();
console.log(now.addMinutes(30));
console.log(now.addWeeks(3));
这些是函数:
Date.prototype.addSeconds = function(seconds) {
this.setSeconds(this.getSeconds() + seconds);
return this;
};
Date.prototype.addMinutes = function(minutes) {
this.setMinutes(this.getMinutes() + minutes);
return this;
};
Date.prototype.addHours = function(hours) {
this.setHours(this.getHours() + hours);
return this;
};
Date.prototype.addDays = function(days) {
this.setDate(this.getDate() + days);
return this;
};
Date.prototype.addWeeks = function(weeks) {
this.addDays(weeks*7);
return this;
};
Date.prototype.addMonths = function (months) {
var dt = this.getDate();
this.setMonth(this.getMonth() + months);
var currDt = this.getDate();
if (dt !== currDt) {
this.addDays(-currDt);
}
return this;
};
Date.prototype.addYears = function(years) {
var dt = this.getDate();
this.setFullYear(this.getFullYear() + years);
var currDt = this.getDate();
if (dt !== currDt) {
this.addDays(-currDt);
}
return this;
};
我觉得这里的许多答案都缺乏创造性的成分,这是时间旅行计算非常需要的。我提出了30分钟时间翻译的解决方案。
(这里jsfiddle)
function fluxCapacitor(n) {
var delta,sigma=0,beta="ge";
(function(K,z){
(function(a,b,c){
beta=beta+"tT";
switch(b.shift()) {
case'3':return z('0',a,c,b.shift(),1);
case'0':return z('3',a,c,b.pop());
case'5':return z('2',a,c,b[0],1);
case'1':return z('4',a,c,b.shift());
case'2':return z('5',a,c,b.pop());
case'4':return z('1',a,c,b.pop(),1);
}
})(K.pop(),K.pop().split(''),K.pop());
})(n.toString().split(':'),function(b,a,c,b1,gamma){
delta=[c,b+b1,a];sigma+=gamma?3600000:0;
beta=beta+"im";
});
beta=beta+"e";
return new Date (sigma+(new Date( delta.join(':')))[beta]());
}
一行代码
var afterSomeMinutes = new Date(new Date().getTime() + minutes * 60000);
在哪里分钟是一个数字