title: Calculate days remaining until next birthday in Javascript
body: <p>I'm trying to calculate remaining days until the next birthday but keeps running into problems. This is the code so far (has some issues with negative days if your birthday has already been this year etc). Any tips on how I can solve this?</p>
  
  <pre><code>    var birthdayDate = new Date(year, month-1, day, 12);
      var now = new Date();
  
      var days = 0;
  
      Math.floor(days = ( (birthdayDate.getTime() - now.getTime())  / (1000*60*60*24)));      
  
  
      if(days &lt; 0)        
      {
          var yearDiff = birthdayDate.getYear() - now.getYear();
          yearDiff *= -1; 
  
          var monthDiff = birthdayDate.getMonth() - now.getMonth();
  
          var daysDiff = birthdayDate.getDay() - now.getDay();
  
  
          if(monthDiff &lt;= 0)
          {
              if(daysDiff &gt; 0)
              {
              }
  
              else
              {
                  days += 365;
              }
          }
  
          var extraDays = yearDiff / 4;
          days = days + (yearDiff * 365) + extraDays;                
      } 
  
      else
      {
          throw new FutureDateException();
      }
  
  
      days = Math.floor(Math.round(days));
  
      if(days === 365)
      {
          days = 0;
      }
  
      if(days === 366)
      {
          days = 1;
      }
  
      return days;
  </code></pre>