简体   繁体   English

Moment.js:日期之间的日期

[英]Moment.js: Date between dates

I'm trying to detect with Moment.js if a given date is between two dates. 我试图用Moment.js检测给定日期是否在两个日期之间。 Since version 2.0.0, Tim added isBefore() and isAfter() for date comparison. 从版本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? 有任何想法吗?

In versions 2.9+ there is an isBetween function, 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)

... which is logically equivalent to ......这在逻辑上等同于
!(x.isBefore(a) || x.isAfter(b))


In version 2.13 the isBetween function 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 null as the third argument to disregard units in which case milliseconds is the default granularity. 使用null作为忽略单位的第三个参数,在这种情况下,毫秒是默认粒度。

Visit the Official Docs 访问官方文档

You can use one of the moment plugin -> moment-range to deal with date 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

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/

I do believe that 我确实相信

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

works too... 也工作......

Good news everyone, there's an isBetween function! 大家好消息,有一个isBetween函数! Update your library ;) 更新你的图书馆;)

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

Please use the 4th parameter of moment.isBetween function (inclusivity). 请使用moment.isBetween函数(包含性)的第4个参数。 Example: 例:

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
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. 如果您只想这样做一次或两次,可能比拉入整个插件更容易。

As Per documentation of moment js, 作为时刻js的Per文档,

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编写的精确范围插件,可用于显示日期/时间范围的精确,人类可读的表示,网址: 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM