日期范围之间的 JavaScript 日期范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17304426/
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-10-27 07:48:37  来源:igfitidea点击:

JavaScript date range between date range

javascriptdate

提问by jfreak53

I'm trying to have an IF check to see if X date range is between Y date range. But it's not returning the correct true/false on the correct time:

我正在尝试进行 IF 检查以查看 X 日期范围是否在 Y 日期范围之间。但它没有在正确的时间返回正确的真/假:

var startdate = new Date('06/06/2013');
var enddate = new Date('06/25/2013');
var startD = new Date('06/08/2013');
var endD = new Date('06/18/2013');

if(startD >= startdate || endD <= enddate) {
  return true;
} else {
  return false;
}

This works, but if I change startdateto 06/09/2013and enddateto 06/17/2013it no longer works while it should work.

此作品,但如果我改变startdate06/09/2013enddate06/17/2013它不再工作,而它应该工作。

It should even work if startdatewas 06/07/2013and enddatewas 06/15/2013, but doesn't. Any thoughts?

如果startdatewas06/07/2013enddatewas 06/15/2013,它甚至应该工作,但没有。有什么想法吗?

回答by voithos

If you're trying to detect full containment, that is fairly easy. (Also, you don't need the explicit return true/false, because the condition is a boolean anyway. Just return it)

如果您试图检测完全遏制,那是相当容易的。(另外,你不需要显式return true/false,因为条件无论如何都是布尔值。只需返回它)

// Illustration:
//
// startdate                          enddate
// v                                        v
// #----------------------------------------#
//
//         #----------------------#
//         ^                      ^
//         startD              endD
return startD >= startdate && endD <= enddate;


An overlap test is slightly more complex. The following will return trueif the two date ranges overlap, regardless of order.

重叠测试稍微复杂一些。true如果两个日期范围重叠,则无论顺序如何,都将返回以下内容。

// Need to account for the following special scenarios
//
// startdate     enddate
// v                v
// #----------------#
//
//         #----------------------#
//         ^                      ^
//         startD              endD
//
// or
//
//              startdate        enddate
//                 v                v
//                 #----------------#
//
//         #------------------#
//         ^                  ^
//       startD              endD
return (startD >= startdate && startD <= enddate) ||
       (startdate >= startD && startdate <= endD);

@Bergi's answer is probably more elegant, in that it just checks the start/end pairs of the two date ranges.

@Bergi 的回答可能更优雅,因为它只检查两个日期范围的开始/结束对。

回答by Bergi

To check if they overlap with any days, use

要检查它们是否与任何日期重叠,请使用

if (endD >= startdate && startD <= enddate)

which is equivalent to

这相当于

if ( !(endD < startdate || startD > enddate)) // not one after the other

回答by Bungus

In your example, the new dates are both outside the range.

在您的示例中,新日期都在范围之外。

If you want to check if there is anyoverlap between the date ranges, use:

如果要检查是否有任何的日期范围,使用重叠:

return (endD >= startdate && startD <= enddate);