title: Javascript days in October
body: <p>I have a funny bug in my calculation about interest days. I go through each day and check which day it is (1 to 31). Now I found a problem: In October the count doesn't work properly. That means the 27th is the 26th, or the 29th is the 28th. Is this a well know problem? 
  Maybe the problem is in my code, but, because it works over another period, it seems to be fine.</p>
  
  <pre><code>    &lt;!DOCTYPE html&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  
      &lt;script type="text/javascript"&gt;
          function berecheZinstage() {
              //eingabe von den Feldern holen
              var strVon = txtVonDatum.value;
              var strBis = txtBisDatum.value;
              //label um das Resultat anzuzeigen
              var resultatTage = document.getElementById("lblTage");
              var resultatZinsTage = document.getElementById("lblZinstage");
              //tag als Milisekunden
              var tagMs = 86400000;
              var monatCount;
              //Eingabeformat umwandeln für die Berechung
              strVon = strVon.replace(/-/g, "/");
              strBis = strBis.replace(/-/g, "/");
              var vonDatum = new Date(strVon);
              var bisDatum = new Date(strBis);
              var zinsTage = 0;
              if (bisDatum &gt; vonDatum) {
                  var totTage = bisDatum - vonDatum;
                  var nDays = Math.round(totTage / (1000 * 60 * 60 * 24));
  
                  var pruefMS = vonDatum.getTime();
                  var startMS = vonDatum.getTime();
                  var endeMS = bisDatum.getTime();
                  var febCount = 0;
                  var langCount = 0;
                  var tage = 0;
                  for (var i = 0; i &lt; nDays; i++) {
                     pruefMS = pruefMS + tagMs;
                     var pruefDatum = new Date(pruefMS);
                      var pruefMonat = pruefDatum.getMonth();
                      var pruefJahr = pruefDatum.getFullYear();
  
                      var pruefTag = pruefDatum.getDate();
  
                      if (pruefTag == 1 &amp;&amp; pruefDatum != startMS) {
                          if (pruefMonat == 2) {
                              var istSchaltjahr = new Date(pruefJahr, 1, 29).getMonth() == 1;
                              if (istSchaltjahr) {
                                  tage++;
                              }
                              else {
                                  tage = tage + 2;
                              }
                          }
                      }
                      if (pruefTag != 31) {
                          tage++;
                      }
                  }
                  resultatZinsTage.innerText = tage.toString();
                  resultatTage.innerText = pruefTag;//nDays.toString();
              }
              else {
                  resultatTage.innerText = "Bis Datum muss grösser sein als von Datum";
              }
  
          }
  
      &lt;/script&gt;
  
      &lt;title&gt;Zinstage berechen&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
      &lt;table style="width:100%;"&gt;
          &lt;tr&gt;
              &lt;td style="width:100px;"&gt;&lt;input id="txtVonDatum" type="text" /&gt;&lt;/td&gt;
              &lt;td style="width:100px;"&gt;&lt;input id="txtBisDatum" type="text" /&gt;&lt;/td&gt;
              &lt;td style="width:100px;"&gt;&lt;button id="btnCalcDays" type="button" onclick="berecheZinstage();"&gt;Berechnen&lt;/button&gt;&lt;/td&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Tage:&lt;/td&gt;
              &lt;td&gt;&lt;label id="lblTage"&gt;&lt;/label&gt;&lt;/td&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Zinstage:&lt;/td&gt;
              &lt;td&gt;&lt;label id="lblZinstage"&gt;&lt;/label&gt;&lt;/td&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Thanks, Marlowe</p>