title: Find closest date in array with JavaScript
body: <p>I have an array with days in it. Each day is an object, for example:</p>
  
  <p><code>{day_year: "2012", day_month: "08", day_number: "03", day_name: "mon"}</code></p>
  
  <p>I have also added a timestamp attribute to each day object, by using:</p>
  
  <pre><code>function convertDays() {
      var max_i = days.length;
      for(var i = 0; i &lt; max_i; i++) {
          var tar_i = days[i];
          tar_i.timestamp = new Date(tar_i.day_year, tar_i.day_month, tar_i.day_number);
      }
  }
  </code></pre>
  
  <p>The days in the array are arbitrary, so there is no real logic to them. </p>
  
  <p>Now I want to find the two closest days to any given date. So if the array with days contains </p>
  
  <ul>
  <li>August 2, 2012</li>
  <li>August 4, 2012</li>
  <li>August 23, 2012</li>
  </ul>
  
  <p>And I search for August 11, 2012, I want it to return August 4, 2012 and August 23, 2012.</p>
  
  <p>I have tried using an answer from another question, that looks like this:</p>
  
  <pre><code>function findClosest(a, x) {
      var lo, hi;
      for(var i = a.length; i--;) {
          if(a[i] &lt;= x &amp;&amp; (lo === undefined || lo &lt; a[i])) lo = a[i];
          if(a[i] &gt;= x &amp;&amp; (hi === undefined || hi &gt; a[i])) hi = a[i];
      }
      return [lo, hi];
  }
  </code></pre>
  
  <p>However, this returns <code>unidentified</code>.</p>
  
  <p>What would be the most efficient (least processor/memory intensive way) to achieve this?</p>
  
  <p><strong>Edit: "However, how are those results "strange"? Could you provide an example of your code and data?"</strong></p>
  
  <p>I'm now using the following to generate an array of dates:</p>
  
  <pre><code>var full_day_array = [];
  for(var i = 0; i &lt; 10; i++) {
      var d = new Date();
      d.setDate(d.getDate() + i);
      full_day_array.push({day_year: d.getFullYear().toString(), day_month: (d.getMonth() + 1).toString(), day_number: d.getDate().toString()});
  }
  </code></pre>
  
  <p>The strange part is, using the code below, this only works for an array of 10 dates or shorter. Whenever I use an array of 11 or more dates, the results become unexpected. </p>
  
  <p>For instance: using an array of 15 dates, starting on August 6, 2012, to August 21, 2012. If I then call <code>findClosest(full_day_array, new Date("30/07/2012");</code> you would expect it to return <code>{nextIndex: 0, prevIndex: -1}</code>. However, it returns <code>{nextIndex: 7, prevIndex: -1}</code>. Why?</p>
  
  <pre><code>function findClosest(objects, testDate) {
      var nextDateIndexesByDiff = [],
          prevDateIndexesByDiff = [];
  
      for(var i = 0; i &lt; objects.length; i++) {
          var thisDateStr = [objects[i].day_month, objects[i].day_number, objects[i].day_year].join('/'),
              thisDate    = new Date(thisDateStr),
              curDiff     = testDate - thisDate;
  
          curDiff &lt; 0
              ? nextDateIndexesByDiff.push([i, curDiff])
              : prevDateIndexesByDiff.push([i, curDiff]);
      }
  
      nextDateIndexesByDiff.sort(function(a, b) { return a[1] &lt; b[1]; });
      prevDateIndexesByDiff.sort(function(a, b) { return a[1] &gt; b[1]; });
  
  
      var nextIndex;
      var prevIndex;
  
      if(nextDateIndexesByDiff.length &lt; 1) {
          nextIndex = -1;
      } else {
          nextIndex = nextDateIndexesByDiff[0][0];
      }
      if(prevDateIndexesByDiff.length &lt; 1) {
          prevIndex = -1;
      } else {    
          prevIndex = prevDateIndexesByDiff[0][0];
      }
      return {nextIndex: nextIndex, prevIndex: prevIndex};
  }
  </code></pre>