title: using JS to return date of next specific day of week, but getting undesired results
body: <p>I have a JS script that fetches the dates of the upcoming Tuesday and Thursday, but noticed today that the Thursday date is way off. I think it has to do with being close to the end of the month of June, but am not sure how to fix. Any ideas?</p>
  
  <p><strong>Current result:</strong></p>
  
  <p>Tuesday, June 30th</p>
  
  <p>Thursday, March 10th</p>
  
  <p><strong>Expected result:</strong></p>
  
  <p>Tuesday, June 30th</p>
  
  <p>Thursday, July 2nd</p>
  
  <p><a href="https://jsfiddle.net/oneelement/pf8gon8w/" rel="nofollow">https://jsfiddle.net/oneelement/pf8gon8w/</a></p>
  
  <pre><code>var today = new Date();
  var dayOfWeek = today.getDay();
  var month = [
      'January',
      'February',
      'March',
      'April',
      'May',
      'June',
      'July',
      'August',
      'September',
      'October',
      'November',
      'December'];
  
  function getNextDate(d) {
      var date = today.getDate();
      while (today.getDay() !== d) {
          today.setDate(++date);
      }
      return today;
  }
  
  var getOrdinal = function (n) {
      var s = ["th", "st", "nd", "rd"],
          v = n % 100;
      return n + (s[(v - 20) % 10] || s[v] || s[0]);
  }; // credit: http://forums.shopify.com/categories/2/posts/29259
  
  var tueDate = getNextDate(2);
  tueDate = 'Tuesday, ' + month[tueDate.getMonth()] + ' ' + getOrdinal(tueDate.getDate());
  var thuDate = getNextDate(4);
  thuDate = 'Thursday, ' + month[thuDate.getMonth()] + ' ' + getOrdinal(thuDate.getDate());
  
  if (dayOfWeek &lt; 3 || dayOfWeek &gt; 4) {
      $('#event1').html(tueDate);
      $('#event2').html(thuDate);
  } else {
      $('#event1').html(thuDate);
      $('#event2').html(tueDate);
  }
  </code></pre>