时间段比较

Time periods compare

本文关键字:比较 时间段      更新时间:2023-09-26

我在javascript中有一个对象的集合,它们代表时间段。我需要迭代并知道每个重叠。必须有一些框架来比较JavaScript中的周期,但我没有给出任何框架,我发现执行这个功能非常困难。

你知道怎么做吗?

例如:

function period (initDate, duration)
{
   this.initDate = initDate;
   this.duration= duration;
}
var now = Date.now();
var date2 = now.addMinutes(90);
var date3 = now.addMinutes(120);
var date4 = now.addMinutes(180);
var date5 = now.addMinutes(240);
var date6 = now.addMinutes(190);
var periods = new Array(); 
periodos.push(new period(now, 30));
periodos.push(new period(date2, 150));
periodos.push(new period(date3, 90));
periodos.push(new period(date4, 45));
periodos.push(new period(date5, 25));
periodos.push(new period(date6, 60));
// So if it is not declared at 13:00 would be:
// period[0] start=13:00  end=13:30
// period[1] start=14:30  end=17:00
// period[2] start=15:00  end=16:30
// period[3] start=16:00  end=16:45
// period[4] start=17:00  end=17:45
// period[5] start=16:10  end=17:10
// They overlap in this example:
// period[0] no overlap
// period[1] with period[2], period[3] y period[5]
// period[2] with period[1], period[3] y period[5]
// period[3] with period[1], period[2] y period[5]
// period[4] with period[5]
// period[5] with period[1], period[2], period[3] y period[4]
//var numPeriodsOverlaps = periodsOverlaps(period[5]);
/*
function periodsOverlaps(periodFind)
{
	// iterate and compare with all
    
}
*/

您需要将每个周期转换为开始和结束时间值,然后查看开始和结束是否在任何其他时间值的开始和结束范围内。这里有一个函数可以做到这一点,希望评论足够。

// From OP
function period (initDate, duration) {
  this.initDate = initDate;
  this.duration= duration;
}
// Function to add minutes to a time value in milliseconds
function addMinutes(timevalue, mins) {
  timevalue += mins * 6e4;
  return timevalue;
}
// Test data
var now = Date.now();
var date2 = addMinutes(now, 90);
var date3 = addMinutes(now, 120);
var date4 = addMinutes(now, 180);
var date5 = addMinutes(now, 240);
var date6 = addMinutes(now, 190);
var periods = [
  new period(now, 30), 
  new period(now, 40),
  new period(date2, 150), 
  new period(date3, 90),
  new period(date4, 45),
  new period(date5, 25),
  new period(date6, 60) 
];
// Check for overlapping periods
function checkOverlap(periods) {
  var overlappingPeriods = [];
  // For each period ...
  periods.forEach(function(p, i) {
    var pStart = p.initDate;
    var pEnd   = addMinutes(pStart, p.duration);
    // Check for overlap with all other periods
    periods.forEach(function(q, j) {
      
      // Don't test against self or previous
      if (j <= i) return; 
      
      var qStart = q.initDate;
      var qEnd   = addMinutes(qStart, q.duration);
    
      // See if start or end is within or entirely wraps previous period
      // Might only overlap by 1 millisecond
      if ((qStart > pStart && qStart < pEnd) || (qEnd > pStart && qEnd < pEnd) ||
          (qStart < pStart && qEnd > pEnd)) {
        overlappingPeriods.push([p, q]);
      }
    });
  });
  return overlappingPeriods
}
// Run the tests
var overlappingPeriods = checkOverlap(periods);
// Show results
var s = '';
overlappingPeriods.forEach(function(a) {
  s += new Date(a[0].initDate).toISOString() + ' plus ' + a[0].duration + ' overlaps with ' +
       new Date(a[1].initDate).toISOString() + ' plus ' + a[1].duration + '<br>';
});
document.write(s);

如果我理解你想要实现的目标,我认为这可能会做到。

我理解你的问题:

将日期传递到函数中。循环访问开始时间和结束时间的数组,并评估传递的日期是否介于开始时间和结束时间之间。

我只是自己在学习JavaScript,所以我依赖CoffeeScript。

以下是 CoffeeScript 版本,我发现它比 JavaScript 更容易阅读:

# Usage: zip(arr1, arr2, arr3, ...)
zip = () ->
  # zips iterables a la Python's zip
  # https://coffeescript-cookbook.github.io/chapters/arrays/zip-function
  lengthArray = (arr.length for arr in arguments)
  length = Math.min(lengthArray...)
  for i in [0...length]
    arr[i] for arr in arguments
MINUTES = 60000  # factor to convert to milliseconds
now = Date.now()  # milliseconds since epoch time
durations = new Array(90, 120, 180, 240, 190)  # minutes
start_times = new Array()
start_times.push(now)
# convert to milliseconds
start_times.push(now + (duration * MINUTES)) for duration in durations
time_periods = new Array(30, 150, 90, 45, 25, 60)  # minutes
end_times = new Array()
for time_periods__start_times in zip(time_periods, start_times)
  time_period = time_periods__start_times[0]
  start_time = time_periods__start_times[1]
  end_times.push(start_time + (time_period * MINUTES))
is_between_periods = (date)->
  for end_time__start_time in zip(end_times, start_times)
    end_time = end_time__start_time[0]
    start_time = end_time__start_time[1]
    overlaps = date >= start_time and date <= end_time
    date = new Date(date)
    start = new Date(start_time)
    end = new Date(end_time)
    console.log("date: #{date}'nstart: #{start}'nend: #{end}'noverlaps: #{overlaps}")
is_between_periods(now)

我将now传递到 is_between_periods 函数中得到的结果如下:

日期:2016 年 2 月 23 日星期二 16:13:38 GMT+0000 (UTC)

开始: 星期二 二月 23 2016 16:13:38 GMT+0000 (UTC)

结束:2016 年 2 月 23 日星期二 16:43:38 GMT+0000 (UTC)

重叠:真

日期:2016 年 2 月 23 日星期二 16:13:38 GMT+0000 (UTC)

开始时间:2016 年 2 月 23 日星期二 17:43:38 GMT+0000 (UTC)

结束:2016 年 2 月 23 日星期二 20:13:38 GMT+0000 (UTC)

重叠:假

日期:2016 年 2 月 23 日星期二 16:13:38 GMT+0000 (UTC)

开始时间:2016 年 2 月 23 日星期二 18:13:38 GMT+0000 (UTC)

结束:2016 年 2 月 23 日星期二 19:43:38 GMT+0000 (UTC)

重叠:假

日期:2016 年 2 月 23 日星期二 16:13:38 GMT+0000 (UTC)

开始: 星期二 二月 23 2016 19:13:38 GMT+0000 (UTC)

结束:2016 年 2 月 23 日星期二 19:58:38 GMT+0000 (UTC)

重叠:假

日期:2016 年 2 月 23 日星期二 16:13:38 GMT+0000 (UTC)

开始: 星期二 二月 23 2016 20:13:38 GMT+0000 (UTC)

结束:2016 年 2 月 23 日星期二 20:38:38 GMT+0000 (UTC)

重叠:假

日期:2016 年 2 月 23 日星期二 16:13:38 GMT+0000 (UTC)

开始: 2016年2月23

日 星期二 19:23:38 GMT+0000 (UTC)

结束:2016 年 2 月 23 日星期二 20:23:38 GMT+0000 (UTC)

重叠:假

这是从 CoffeeScript 生成的 JavaScript:

// Generated by CoffeeScript 1.10.0
var MINUTES, duration, durations, end_times, is_between_periods, j, k, len, len1, now, ref, start_time, start_times, time_period, time_periods, time_periods__start_times, zip;
zip = function() {
  var arr, i, j, length, lengthArray, ref, results;
  lengthArray = (function() {
    var j, len, results;
    results = [];
    for (j = 0, len = arguments.length; j < len; j++) {
      arr = arguments[j];
      results.push(arr.length);
    }
    return results;
  }).apply(this, arguments);
  length = Math.min.apply(Math, lengthArray);
  results = [];
  for (i = j = 0, ref = length; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) {
    results.push((function() {
      var k, len, results1;
      results1 = [];
      for (k = 0, len = arguments.length; k < len; k++) {
        arr = arguments[k];
        results1.push(arr[i]);
      }
      return results1;
    }).apply(this, arguments));
  }
  return results;
};
MINUTES = 60000;
now = Date.now();
durations = new Array(90, 120, 180, 240, 190);
start_times = new Array();
start_times.push(now);
for (j = 0, len = durations.length; j < len; j++) {
  duration = durations[j];
  start_times.push(now + (duration * MINUTES));
}
time_periods = new Array(30, 150, 90, 45, 25, 60);
end_times = new Array();
ref = zip(time_periods, start_times);
for (k = 0, len1 = ref.length; k < len1; k++) {
  time_periods__start_times = ref[k];
  time_period = time_periods__start_times[0];
  start_time = time_periods__start_times[1];
  end_times.push(start_time + (time_period * MINUTES));
}
is_between_periods = function(date) {
  var end, end_time, end_time__start_time, l, len2, overlaps, ref1, results, start;
  ref1 = zip(end_times, start_times);
  results = [];
  for (l = 0, len2 = ref1.length; l < len2; l++) {
    end_time__start_time = ref1[l];
    end_time = end_time__start_time[0];
    start_time = end_time__start_time[1];
    overlaps = date >= start_time && date <= end_time;
    date = new Date(date);
    start = new Date(start_time);
    end = new Date(end_time);
    results.push(console.log("date: " + date + "'nstart: " + start + "'nend: " + end + "'noverlaps: " + overlaps));
  }
  return results;
};
is_between_periods(now);

你试过了吗.js,它有很多功能来处理日期、时间和时区插件。