title: How do I add and subtract months from a date?
body: <p>I am trying to add/subtract an integer to the element of array. my function is </p>
  
  <pre><code>var addToMonth = function (date, monthsToAdd) {
      var tempDate = date.split("-");
      tempDate[1] = parseInt(tempDate[1]) + monthsToAdd;
      tempDate[1] = 0 + tempDate[1];
      console.log("TempDate "+tempDate[1]);
      tempDate = tempDate.join("-");
      console.log("Added TempDate: "+tempDate);
      return tempDate;
  }
  </code></pre>
  
  <p>The acceptable date string is: <code>date = "2016-04-05"</code>. If call the function as <code>addToMonth(date,1)</code>. The output is correct that is <code>2016-05-05</code>. But when I call it as <code>addToMonth(date, -1)</code>. It doesn't work. What is the correct way to do it?</p>