【问题标题】:How do I find the nearest Past or Future Date in an array of Dates by an DateObject?如何通过 DateObject 在日期数组中找到最近的过去或未来日期?
【发布时间】:2021-09-07 21:28:02
【问题描述】:

假设我们有一个日期数组

var dateArr = [new Date("Thu Apr 01 2021 00:00:00 GMT+0200"), new Date("Sat May 01 2021 00:00:00 GMT+0200")];

还有一个Date Object,我们需要在dateArr中搜索,例如:

var findDate = new Date("Mon Apr 05 2021 07:50:06 GMT+0200");

我们一起拥有这个 PLUS

  • 返回最近日期的函数 dateArr 通过 findDate 可以位于过去或未来
var dateArr = [new Date("Thu Apr 01 2021 00:00:00 GMT+0200"), new Date("Sat May 01 2021 00:00:00 GMT+0200")];

var findDate = new Date("Mon Apr 05 2021 07:50:06 GMT+0200");

var result = getNearestDateInDateArrByFindDate(dateArr, findDate);

console.log(result); //should print to console: Thu Apr 01 2021 00:00:00 GMT+0200

function getNearestDateInDateArrByFindDate(dateArr, findDate) {
   var nearestDateInPastOrFuture;

   ...

   return nearestDateInPastOrFuture;
}

到目前为止我尝试过的事情都没有成功......

var dateArr = [new Date("Thu Apr 01 2021 00:00:00 GMT+0200"), new Date("Sat May 01 2021 00:00:00 GMT+0200")];

var findDate = new Date("Mon Apr 05 2021 07:50:06 GMT+0200");

function getNearestDateInDateArrByFindDate(dateArr, findDate) {

        console.log(dateArr);
        console.log(findDate);

        var nearestFutureDates = dateArr.filter(dt => dt.getTime() >= findDate.getTime());
        var nearestFutureDates = nearestFutureDates.sort((a, b) => a.getTime() - b.getTime());

        var nearestPastOrFutureDate = dateArr.filter(dt => dt.getTime() >= findDate.getTime());
        var nearestPastOrFutureDate = nearestPastOrFutureDate.sort((a, b) => (findDate.getTime() - a.getTime()) - (findDate.getTime() - b.getTime()));

        console.log(nearestFutureDates);
        console.log(nearestPastOrFutureDate);
  //returns always sat May 01 2021 00:00:00 GMT+0200
}


getNearestDateInDateArrByFindDate(dateArr, findDate)

不知何故,sn-p 不返回 4 月 1 日,而是 4 月 31 日?

【问题讨论】:

  • 如果你正在使用时刻检查这个:stackoverflow.com/a/62380664/5909026
  • 我给你做了一个sn-p。如有需要请更正
  • 现在试过了,也给我返回了错误的结果:sat May 01 2021 00:00:00 GMT+0200 它似乎只关注未来的日期,但我也需要过去的日期!
  • 大部分不相关,但“Thu Apr 01 2021 00:00:00 GMT+0200”是一种非标准日期格式。解释将取决于实现,并且不能保证在所有环境中产生相同的日期。

标签: javascript jquery date datetime


【解决方案1】:

我们可以使用Array.sort()按照每个日期的毫秒差排序到findDate。

注意:我们可以使用

获得两个日期之间以毫秒为单位的绝对差
Math.abs(date1 - date2);

所以我们将使用它来排序:

var dateArr = [new Date("Thu Apr 01 2021 00:00:00 GMT+0200"), new Date("Sat May 01 2021 00:00:00 GMT+0200")];

var findDate = new Date("Mon Apr 05 2021 07:50:06 GMT+0200");

var result = getNearestDateInDateArrByFindDate(dateArr, findDate);

console.log(result); //should print to console: Thu Apr 01 2021 00:00:00 GMT+0200

function getNearestDateInDateArrByFindDate(dateArr, findDate) {
    const sortedByDiff = [...dateArr].sort((a,b) => {
        // Sort by the absolute difference in ms between dates.
        return Math.abs(a - findDate) - Math.abs(b - findDate);
    })
    // Return the first date (the one with the smallest difference)
    return sortedByDiff[0];
}

【讨论】:

  • 像魅力一样工作!谢谢!
【解决方案2】:

您可以过滤过去最近的日期和未来的最近日期。

如果没有订购,您可以选择将.sort((a, b) => a - b) 应用于您的日期数组。

const dateArr = [new Date("Thu Apr 01 2021 00:00:00 GMT+0200"), new Date("Sat May 01 2021 00:00:00 GMT+0200")].sort((a, b) => a - b);
const findDate = new Date("Mon Apr 05 2021 07:50:06 GMT+0200");

const nearestPastDate = (dateArr, date) => {
    const pastArr = dateArr.filter(n => n <= date);
    return pastArr.length > 0 ? pastArr[pastArr.length-1]: null;
};

const nearestFutureDate = (dateArr, date) => {
    const futArr = dateArr.filter(n => n >= date);
    return futArr.length > 0 ? futArr[0]: null;
};

console.log(dateArr);
console.log(nearestPastDate(dateArr, findDate));
console.log(nearestFutureDate(dateArr, findDate));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    相关资源
    最近更新 更多