2016-12-14 21 views
-1

入力値に複数の日付(strtotime)があり、jQueryまたはjavascriptを使用して指定した日付(my date)私は何をしますか?指定された日付の後に最も近い日付を取得する

<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

PHPのstrtotime: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

私は、指定された日付(私の日付)の後にある最も近い日付が欲しいです。 –

関連する問題