View Single Post
Old 11-16-2012, 05:23 AM   PM User | #11
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
*sigh*
Code:
var mDays = [ 31,28,31,30,31,30,31,31,20,31,30,31 ];
// handle leap year:
mDays[1] = isLeapYear ? 29 : 28;

var theMonth = -1;
for ( var m = 0; m < 12; ++m )
{
    if ( days <= mDays[m] )
    {
        theMonth = m;
        break; // out of for loop
    }
    days -= mdays{m];
}
var monthName = names[theMonth];
But there is a problem with that, as you will discover.

Logically, day ZERO of each year should be Jan 1st.

Probably you can handle that by first subtracting off the number of days that correspond to the correct year and then adding back 1 day before entering the above code. Probably. Untested.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote