获取日期对象池的下一个即将到来的日期

Getting the next upcoming date of a pool of date-objects

本文关键字:下一个 日期 即将到来 取日期 对象 获取      更新时间:2023-09-26

我必须在我的网站上显示下一个事件。没有可用的数据库。我的想法是:用日期对象创建一个javascript数组,然后我需要一个函数来说:给我创建的DateObject数组的下一个即将到来的日期。我该怎么做?

使用我在简单搜索后发现的这个二进制搜索函数,并无耻地从Code Review中复制,您可以向它传递事件数组和明天的日期(Date(today.getTime() + (24 * 60 * 60 * 1000)))。

function binarySearch(arr, find) 
{
  var low = 0, high = arr.length - 1, i, comparison;
  while (low <= high) 
  {
    i = Math.floor((low + high) / 2);
    if (arr[i] < find) 
    { 
       low = i + 1;
    }
    if (arr[i] > find)
    { 
       high = i - 1; 
    }
    return i;
  }
  return null;
}
var currentEvent = binarySearch(eventsArray, Date(today.getTime() + (24 * 60 * 60 * 1000)));
//code to display eventsArray[currentEvent] here