2016-12-14 85 views
-1

我在输入值中有多个日期(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

什么是“strtotime”? –

+0

strtotime in php,:http://php.net/manual/en/function.strtotime.php –

+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); 
+0

注意,没有值的输入将返回值-1。 –

+0

tnx,但是这给了我结果'正确的价值1481691600',万一离'1481778000 =>(2016-12-15)'最近的日期是:'1482037200 =>(2016-12-18)''。请参阅:https://jsfiddle.net/gmmn4erq/ –

+0

我想要在指定日期(我的日期)之后的最近日期。 –