Page 1 of 1

javascript week for month

Posted: Sun Jun 01, 2008 7:42 am
by kendall
Hey,

There have been alot of snippets that deal with getting the week of a year. But has anyone ever come up with a week of the month?

Kendall

Re: javascript week for month

Posted: Sun Jun 01, 2008 11:42 am
by kaszu

Re: javascript week for month

Posted: Sun Jun 01, 2008 11:51 am
by kendall
didnt work

Re: javascript week for month

Posted: Sun Jun 01, 2008 2:43 pm
by kaszu
Wrote my own function:

Code: Select all

function getWeekOfMonth(now) {
    var todayDayOfMonth = now.getDate() - 1;
    
    var first = new Date(now.getFullYear() + '/' + (now.getMonth() + 1) + '/01');
    var monthFirstDateDay = first.getDay();
    
    return Math.ceil((todayDayOfMonth + monthFirstDateDay) / 7);
}
    
for(var i=1; i<10; i++)
    document.write('2008/05/0' + i + ': ' + getWeekOfMonth(new Date('2008/05/0' + i)) + '<br />');
for(var i=10; i<32; i++)
    document.write('2008/05/' + i + ': ' + getWeekOfMonth(new Date('2008/05/' + i)) + '<br />');