How to get the week in a month for a date with Javascript

Working on my calendar application and I needed to get what week in the current month a day occurs on. Unfortunately I couldn’t find anything like this in the JavaScript Date object so this is the function I came up with

Date.prototype.getMonthWeek = function(){
	var firstDay = new Date(this.getFullYear(), this.getMonth(), 1).getDay();
	return Math.ceil((this.getDate() + firstDay)/7);
}

Thanks to mutilator for helping out with the actual math to figure it out.

This entry was posted in Programming, Tutorials, Web Development and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>