title: Day overflows in javascript Date constructor
body: <p>I am currently writing some sort of javascript based client calendar and observed some issues. All over the net, I can find code samples, where people use day overflows in the Date constructor.</p>
  
  <p>i.e.</p>
  
  <pre><code>// get the first day of the next month
  var myDate = new Date(someDate.getFullYear(),someDate.getMonth(),32);
  myDate.setDate(1);
  </code></pre>
  
  <p>The general idea of this concept is, that since there is no month with 32 days, the constructor will create a date within the next month. I saw even codesample with negative overflows:</p>
  
  <p>i.e.</p>
  
  <pre><code>// get the last day of the previous month
  var myDate = new Date(someDate.getFullYear(),someDate.getMonth(),1);
  myDate.setDate(-1);
  </code></pre>
  
  <p>Now while this seems to work in many cases, I finally found a contradiction:</p>
  
  <pre><code>// this prints "2012-12-30" expected was "2012-12-31"
  var myDate = new Date(2013,0,1);
  myDate.setDate(-1);
  </code></pre>
  
  <p>Further examination finally revealed that dates like
  new Date(2013,0,23) or new Date(2013,0,16) combined with setDate(-1) all end up in "2012-12-31". Finally I observed that using -1 seems to subtract two days (for getting the expected result setDate(0) has to be used).</p>
  
  <p>Is this a bug in the browser implementations or are the code samples spread accross the internet crap??</p>
  
  <p>Furthermore, is this setDate with positive and negative overflow secure to be used and uniformly implemented by all major browsers?</p>