How can i get the week number of month using javascript / jquery?
For ex.:
First Week: 5th July, 2010. / Week Number = First monday
function weekNumberForDate(date){
var janOne = new Date(date.getFullYear(),0,1);
var _date = new Date(date.getFullYear(),date.getMonth(),date.getDate());
var yearDay = ((_date - janOne + 1) / 86400000);//60 * 60 * 24 * 1000
var day = janOne.getUTCDay();
if (day<4){yearDay+=day;}
var week = Math.ceil(yearDay/7);
return week;
}
Apparently the first week of the year is the week that contains that year's first Thursday.
Without calculating the UTCDay, the returned week was one week shy of what it should have been. Not confident this can't be improved, but seems to work for now.