javascript week for month

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

javascript week for month

Post 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
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: javascript week for month

Post by kaszu »

User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Re: javascript week for month

Post by kendall »

didnt work
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: javascript week for month

Post 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 />');
 
Post Reply