day of the year to date in a loop
<p>I am trying to get date from day of the month in a loop.  The code works for the first few days, and from then on it is returning random dates.  </p>

<p>Any help here?</p>

<pre><code>var now = new Date();
var initDate = new Date(now.getYear() + 1900, 0);

for (var i = 1 ; i &lt;366 ; i++) {
    var day = dateFromDay(i, initDate);
    document.write(" Day " + i + " : " + day);
    document.write("&lt;br&gt;");
}

function dateFromDay(day, initDate) {
    var months = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", 
                   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];

    var cur = new Date(initDate.setDate(day));
    cur = months[cur.getMonth()] + " " + cur.getDate();
    return cur;
}
</code></pre>

<p>Output:</p>

<pre><code>...
...
Day 29 : Jan 29
Day 30 : Jan 30
Day 31 : Jan 31
Day 32 : Feb 1
// From here the output gets messed  up
Day 33 : Mar 4
Day 34 : Apr 3
Day 35 : May 5
Day 36 : Jun 5
Day 37 : Jul 7
Day 38 : Aug 7
Day 39 : Sep 8
Day 40 : Oct 10
Day 41 : Nov 10
Day 42 : Dec 12
Day 43 : Jan 12
Day 44 : Feb 13
...
...
</code></pre>
