数组中的日期范围比较

时间:2018-02-05 15:26:19

标签: javascript arrays date date-comparison

我正在尝试比较数组中的日期以便重叠但有问题。用户可以添加他们想要的日期,我需要检查它们以确保没有重叠。

日期数组(开始和结束日期与它们之间的( - )结合:

dates (4) ["02/07/2018-02/07/2018", "02/05/2018-02/07/2018", "02/06/2018-02/06/2018", "02/08/2018-02/08/2018"]

我的代码:

        function CheckOverlappingDates(dates) {
        var startDate = []; 
        var endDate = [];
        var isOverlap;
        for (var i = 0; i < dates.length; i++) {
            var split = dates[i].split('-');  // just split once
            startDate.push(new Date(split[0]).toLocaleDateString());
            endDate.push(new Date(split[1]).toLocaleDateString());

            for (var x = 0; x < startDate.length; x++) {
                if (startDate[x] >= startDate[x + 1] && startDate[x + 1] <= endDate[x + 1]) {
                    isOverlap = true;
                } else {
                    isOverlap = false;
                }
            }

        }

    }

我把它们分成了startdate和enddate,如下面的

enter image description here

但我的功能不比较多个日期范围。

2 个答案:

答案 0 :(得分:1)

您可以使用function wait(ms) { var start = Date.now(), now = start; while (now - start < ms) { now = Date.now(); } } for (var i = 0; i < 3; i++) { alert(i); for (var j = 0; j < 3; j++) { alert(j); wait(2000); } } 循环进行检查。

&#13;
&#13;
for
&#13;
&#13;
&#13;

答案 1 :(得分:1)

比较日期字符串时,可以将它们转换为Date对象或将它们保存为字符串并使用 localeCompare 。不要使用内置的Date解析器,因为它不可靠。 2行函数可以将格式设置为字符串或转换为日期,字符串可能更有效,但比较日期的代码更少。

要查看范围数组是否有重叠,需要将每个范围与数组中的范围进行比较。因为已经完成了,所以不需要检查那些。

不清楚重叠范围应该怎么做,所以这个函数只返回一个字符串数组,其中&#34;范围A重叠范围B&#34;。它还假设范围包括在内,因此02/05 / 2018-02 / 07/2018重叠02/07/2011-02 / 08/2018。

e.g。

&#13;
&#13;
var dates = ["02/07/2018-02/07/2018", "02/05/2018-02/07/2018", "02/06/2018-02/06/2018", "02/08/2018-02/08/2018"];

// Assume array of ranges in format mm/dd/yyyy-mm/dd/yyyy
// Return array of overlapping ranges
function inRange(dates) {

  // Parse date string to Date
  function fn(s) {
    var b = s.split(/\D/);
    return new Date(b[2], b[0]-1, b[1]);
  }
  
  // Return true if range a overlaps range b
  // where range is string in format mm/dd/yyyy-mm/dd/yyyy
  // Overlaps if start or end of b are inside a, or
  // a is wholly inside b
  function overlaps(a, b) {
    a = a.split('-').map(fn);
    b = b.split('-').map(fn);
    return (b[0] >= a[0] && b[0] <= a[1]) || // b start in a
           (b[1] >= a[0] && b[1] <= a[1]) || // b end in a
           (b[0] <= a[0] && b[1] >= a[1]);   // b encloses a
  }
  
  // Array for overlapping ranges
  var overlappers = [];
  var max = dates.length - 1;

  dates.forEach(function(date, i) {
  
    // Don't test last as already tested
    if (i < max) {
    
      // Only test from this element to end of array
      for (var j = i+1; j <= max; j++) {
      
        // If overlaps, add to overlappers array
        if (overlaps(date, dates[j])) {
          overlappers.push(date + ' overlaps ' + dates[j]);
        }
      }
    }
  });
  return overlappers;
}

console.log(inRange(dates));
&#13;
&#13;
&#13;

简单解析器期望日期有效。如果需要检查它们,解析器中需要额外的代码行,并且需要在main函数中处理无效的日期。