获取指定日期之后的最近日期

Getting nearest date that is after the specified date

我在一个输入值中有几个日期(strtotime),并且想要在指定日期(我的日期)之后最近的日期 jQuery 或 javascript。我该怎么办?

<input id="DateBox" value="1481691600,1482037200,1482642000">

我的约会对象:

1481778000 => (2016-12-15)

几个日期(strtotime):

1481691600 => (2016-12-14)
1482037200 => (2016-12-18)
1482642000 => (2016-12-25)

return:

1482037200 => (2016-12-18)

这应该能让您接近您的需要。本质上,您需要遍历每个值并将其与当前日期进行比较,同时存储最佳值。如果我的任何语法有点不对,请原谅我。

current_date = 1481778000;
// Get our values as an array.
var all_values = jQuery('#DateBox').val().split(',');
// Track our current value and difference.
var current_val = -1;
var current_diff = -1;
jQuery.each(all_values, function(index, value) {
    if(current_val == -1) {
        // First value is automatically the best guess.
        current_val = value;
        current_diff = Math.abs(current_date - value);
    } else {
        // Compare against our current best guess.
        if(Math.abs(current_date - value) < current_diff)
            current_val = value;
    }
});
//We have our value.
console.log("Correct Value", current_val);