Javascript Moment.js:日期之间的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14897571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 18:15:28  来源:igfitidea点击:

Moment.js: Date between dates

javascriptdatemomentjs

提问by Joel A. Villarreal Bertoldi

I'm trying to detect with Moment.js if a given date is between two dates. Since version 2.0.0, Tim added isBefore()and isAfter()for date comparison.

我正在尝试使用 Moment.js 检测给定日期是否在两个日期之间。从 2.0.0 版本开始,Tim 添加了isBefore()isAfter()用于日期比较。

Since there's no isBetween()method, I thought this would work:

由于没有isBetween()方法,我认为这会起作用:

var date = moment("15/02/2013", "DD/MM/YYYY");
var startDate = moment("12/01/2013", "DD/MM/YYYY");
var endDate = moment("15/01/2013", "DD/MM/YYYY");

if (date.isBefore(endDate) && date.isAfter(startDate) || (date.isSame(startDate) || date.isSame(endDate)) ) { alert("Yay!"); } else { alert("Nay! :("); }

I'm convinced there's got to be a better way to do this. Any ideas?

我相信必须有更好的方法来做到这一点。有任何想法吗?

采纳答案by Lukasz Koziara

You can use one of the moment plugin-> moment-rangeto deal with date range:

您可以使用moment plugin-> moment-range 之一来处理日期范围:

var startDate = new Date(2013, 1, 12)
  , endDate   = new Date(2013, 1, 15)
  , date  = new Date(2013, 2, 15)
  , range = moment().range(startDate, endDate);

range.contains(date); // false

回答by ThisClark

In versions 2.9+there is an isBetweenfunction, but it's exclusive:

2.9+ 版本中有一个isBetween函数,但它是独占的:

var compareDate = moment("15/02/2013", "DD/MM/YYYY");
var startDate   = moment("12/01/2013", "DD/MM/YYYY");
var endDate     = moment("15/01/2013", "DD/MM/YYYY");

// omitting the optional third parameter, 'units'
compareDate.isBetween(startDate, endDate); //false in this case

There is an inclusive workaround ...
x.isBetween(a, b) || x.isSame(a) || x.isSame(b)

有一个包容性的解决方法......
x.isBetween(a, b) || x.isSame(a) || x.isSame(b)

... which is logically equivalent to
!(x.isBefore(a) || x.isAfter(b))

...这在逻辑上等同于
!(x.isBefore(a) || x.isAfter(b))


In version 2.13the isBetweenfunction has a fourth optional parameter, inclusivity.


2.13 版本中,isBetween函数具有第四个可选参数inclusivity.

Use it like this:

像这样使用它:

target.isBetween(start, finish, 'days', '()') // default exclusive
target.isBetween(start, finish, 'days', '(]') // right inclusive
target.isBetween(start, finish, 'days', '[)') // left inclusive
target.isBetween(start, finish, 'days', '[]') // all inclusive

More units to consider: years, months, days, hours, minutes, seconds, milliseconds

需要考虑的更多单位: years, months, days, hours, minutes, seconds, milliseconds

Note: units are still optional. Use nullas the third argument to disregard units in which case milliseconds is the default granularity.

注意:单位仍然是可选的。使用null作为第三个参数忽略单位在这种情况下毫秒是默认的粒度。

Visit the Official Docs

访问官方文档

回答by Vijay Maheriya

You can use

您可以使用

moment().isSameOrBefore(Moment|String|Number|Date|Array);
moment().isSameOrAfter(Moment|String|Number|Date|Array);

or

或者

moment().isBetween(moment-like, moment-like);

See here : http://momentjs.com/docs/#/query/

在这里看到:http: //momentjs.com/docs/#/query/

回答by Tiele Declercq

I do believe that

我相信

if (startDate <= date && date <= endDate) {
  alert("Yay");
} else {
  alert("Nay! :("); 
}

works too...

也能用...

回答by Luna

Good news everyone, there's an isBetweenfunction! Update your library ;)

大家好消息,有一个isBetween功能!更新你的图书馆;)

http://momentjs.com/docs/#/query/is-between/

http://momentjs.com/docs/#/query/is-between/

回答by Raffael Bechara Rameh

Please use the 4th parameter of moment.isBetween function (inclusivity). Example:

请使用 moment.isBetween 函数的第 4 个参数(包含性)。例子:

var startDate = moment("15/02/2013", "DD/MM/YYYY");
var endDate = moment("20/02/2013", "DD/MM/YYYY");

var testDate = moment("15/02/2013", "DD/MM/YYYY");

testDate.isBetween(startDate, endDate, 'days', true); // will return true
testDate.isBetween(startDate, endDate, 'days', false); // will return false

回答by Jamie Humphries

if (date.isBefore(endDate) 
 && date.isAfter(startDate) 
 || (date.isSame(startDate) || date.isSame(endDate))

is logically the same as

在逻辑上是一样的

if (!(date.isBefore(startDate) || date.isAfter(endDate)))

which saves you a couple of lines of code and (in some cases) method calls.

这为您节省了几行代码和(在某些情况下)方法调用。

Might be easier than pulling in a whole plugin if you only want to do this once or twice.

如果您只想这样做一两次,可能比拉入整个插件更容易。

回答by Mohammed Safeer

As Per documentation of moment js,

根据moment js的文档,

There is Precise Range plugin, written by Rob Dawson, can be used to display exact, human-readable representations of date/time ranges, url :http://codebox.org.uk/pages/moment-date-range-plugin

有由 Rob Dawson 编写的 Precise Range 插件,可用于显示日期/时间范围的准确、人类可读的表示,网址:http: //codebox.org.uk/pages/moment-date-range-plugin

moment("2014-01-01 12:00:00").preciseDiff("2015-03-04 16:05:06");
// 1 year 2 months 3 days 4 hours 5 minutes 6 seconds

moment.preciseDiff("2014-01-01 12:00:00", "2014-04-20 12:00:00");
// 3 months 19 days