我想获得一个日期对象,它比另一个日期对象晚30分钟。我如何用JavaScript做到这一点?
当前回答
我觉得这里的许多答案都缺乏创造性的成分,这是时间旅行计算非常需要的。我提出了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]());
}
其他回答
我总是创建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]());
}
这是IsoString的版本:
console.log(new Date(new Date().setMinutes(new Date().getMinutes() - (30))).toISOString());
也许像这样?
var d = new Date(); var v = new Date(); v.setMinutes (d.getMinutes () + 30); console.log (v)
您应该获取当前日期的值以获得日期(ms),并向其添加(30 * 60 *1000)。现在你有(当前日期+ 30分钟)ms
console.log('with ms', Date.now() + (30 * 60 * 1000)) console.log('new Date', new Date(Date.now() + (30 * 60 * 1000)))
推荐文章
- 如何减去X天从一个日期对象在Java?
- 如何在JavaScript中获得时区名称?
- 在JSON键名中哪些字符是有效的/无效的?
- jQuery中的live()转换为on()
- 如何区分鼠标的“点击”和“拖动”
- IE9是否支持console.log,它是一个真实的功能吗?
- Node.js同步执行系统命令
- 如何转义JSON字符串包含换行字符使用JavaScript?
- jQuery等价于JavaScript的addEventListener方法
- jQuery需要避免的陷阱
- JavaScript中变量字符串的XML解析
- 'React'指的是一个UMD全局,但当前文件是一个模块
- 为什么useState不触发重新渲染?
- 如何使用回调与useState挂钩在反应
- 网络请求失败