我在输入值中有几个日期(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)
返回:
1482037200 => (2016-12-18)
答案 0 :(得分:0)
这可以让你接近你需要的东西。基本上,您需要迭代每个值并将其与当前日期进行比较,随时存储最佳值。请原谅我,如果我的任何一种语法都有点过时的话。
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);