我想获得一个日期对象,它比另一个日期对象晚30分钟。我如何用JavaScript做到这一点?


当前回答

简单地,你可以在momnet库中使用这段代码:

console.log(moment(moment()).add(30,"minutes").format('MM/DD/YYYY hh:mm:ss'));

其他回答

这是IsoString的版本:

console.log(new Date(new Date().setMinutes(new Date().getMinutes() - (30))).toISOString());

我总是创建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;
};

停止使用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()
}

这就是我所做的,似乎很有效:

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));

你可以这样做:

let 30tyminutes = 30 * 60 * 1000;//将30分钟转换为毫秒 let date1 = new Date(); let date2 = new Date(date1.getTime() + 30tyminutes); console.log (date1); console.log (date2);