我想获得一个日期对象,它比另一个日期对象晚30分钟。我如何用JavaScript做到这一点?
当前回答
对于像我这样的懒人:
Kip的答案(从上面)在coffeescript中,使用“enum”,并对同一对象进行操作:
Date.UNIT =
YEAR: 0
QUARTER: 1
MONTH: 2
WEEK: 3
DAY: 4
HOUR: 5
MINUTE: 6
SECOND: 7
Date::add = (unit, quantity) ->
switch unit
when Date.UNIT.YEAR then @setFullYear(@getFullYear() + quantity)
when Date.UNIT.QUARTER then @setMonth(@getMonth() + (3 * quantity))
when Date.UNIT.MONTH then @setMonth(@getMonth() + quantity)
when Date.UNIT.WEEK then @setDate(@getDate() + (7 * quantity))
when Date.UNIT.DAY then @setDate(@getDate() + quantity)
when Date.UNIT.HOUR then @setTime(@getTime() + (3600000 * quantity))
when Date.UNIT.MINUTE then @setTime(@getTime() + (60000 * quantity))
when Date.UNIT.SECOND then @setTime(@getTime() + (1000 * quantity))
else throw new Error "Unrecognized unit provided"
@ # for chaining
其他回答
你可以这样做:
let 30tyminutes = 30 * 60 * 1000;//将30分钟转换为毫秒 let date1 = new Date(); let date2 = new Date(date1.getTime() + 30tyminutes); console.log (date1); console.log (date2);
var myDate= new Date();
var MyNewDate = new Date
(myDate.getFullYear(),myDate.getMonth(),myDate.getDate(),myDate.getMinutes()+10,01,01)
我总是创建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分钟的一种方法是创建第二个日期对象(主要用于演示),并将分钟设置为分钟+ 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 ());
我觉得这里的许多答案都缺乏创造性的成分,这是时间旅行计算非常需要的。我提出了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]());
}
推荐文章
- JavaScript在数组中
- 在Java中转换字符串到日历对象
- 如何从JavaScript中使用正则表达式的字符串中剥离所有标点符号?
- 如何修改标签文本?
- 如何获得十六进制颜色值而不是RGB值?
- 字符串strip() JavaScript?
- 如何将一个标题转换为jQuery的URL段塞?
- 获取对象的第一个索引
- 我如何用javascript编程点击链接?
- JavaScript:客户端验证与服务器端验证
- React钩子:从回调中访问最新状态
- 使用lodash将对象转换为数组
- 打印在 JsFiddle 中
- AngularJS只适用于单页应用程序吗?
- Javascript和regex:分割字符串并保留分隔符