我想获得一个日期对象,它比另一个日期对象晚30分钟。我如何用JavaScript做到这一点?
当前回答
没有实用程序的一行程序:
new Date(+new Date() + 60000*15) // +15 minutes
其他回答
我总是创建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;
};
新日期() var newDateObj =新日期(); (+ 30 * 60 * 1000); 游戏机。log (newDateObj);
我觉得这里的许多答案都缺乏创造性的成分,这是时间旅行计算非常需要的。我提出了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]());
}
停止使用Moment.js
正如其他优秀答案所建议的那样,在大多数情况下,处理日期时最好使用库。然而,重要的是要知道,自2020年9月起,Moment.js被认为是遗留的,不应再用于新项目。
引用Moment在官方文件中的声明:
我们不希望在未来的新项目中使用Moment。[…我们现在通常认为Moment是一个处于维护模式的遗留项目。它还没死,但已经完成了。
现代图书馆
以下是Moment推荐的替代方案。
勒克桑
《Luxon》可以看作是《Moment》的演变。作者Isaac Cambron是Moment的长期贡献者。请阅读卢克松为什么存在?以及Luxon文档中的For Moment用户页面。
locale:提供的Intl 时区:提供国际时区
import {DateTime} from 'luxon'
function addMinutes(date, minutes) {
return DateTime.fromJSDate(date).plus({minutes}).toJSDate()
}
Day.js
Day.js被设计成一个极简的Moment.js替代品,使用类似的API。它不是一个临时的替代品,但如果你习惯了使用Moment的API,想要快速移动,可以考虑使用Day.js。
区域设置:可以单独导入的自定义数据文件 时区:国际提供,通过一个插件
import dayjs from 'dayjs'
function addMinutes(date, minutes) {
return dayjs(date).add(minutes, 'minutes').toDate()
}
日期-fns
Date-fns提供了一系列用于操作JavaScript Date对象的函数。要了解更多详细信息,请滚动到date-fns主页上的“为什么是date-fns?”
区域设置:可以单独导入的自定义数据文件 时区:通过单独的配套库提供了Intl
import {addMinutes} from 'date-fns'
function addMinutesDemo(date, minutes) {
return addMinutes(date, minutes)
}
js-Joda
js-Joda是Java的Three-Ten Backport的JavaScript端口,它是Java SE 8 Java的JSR-310实现的基础。包的时间。如果你熟悉java。time, joda time,或Noda time,你会发现js-Joda相当。
区域:通过附加模块自定义数据文件 时区:通过附加模块自定义数据文件
import {LocalDateTime, nativeJs, convert} from '@js-joda/core'
function addMinutes(date, minutes) {
return convert(
LocalDateTime.from(
nativeJs(date)
).plusMinutes(minutes)
).toDate()
}
var now = new Date(); now.setMinutes(now.getMinutes() + 30);/ /时间戳 日期(现在);//日期对象 console.log(现在);