0

how to find how much number of weeks are there in a month, i.e either a month is having 4 or 5 weeks , how it can be calculated from current date supplied.

Thanks

V.V
  • 3,082
  • 7
  • 49
  • 83
  • 1
    A month can never have 5 whole weeks. The biggest months (31 days) is 4 weeks and 3 days. And the smallest months (28 days) is exactly 4 weeks. So every month has at least 4 weeks, and never as many as 5. Perhaps your question needs more detail about what you really want to calculate... Do you perhaps mean to count something like the number of "Mondays" in a month? – Alex Wayne Jan 13 '12 at 02:00
  • I needed to show weeks on graph so if there are more that 4 weeks i.e. 4 week and 3 days then also need to show as Week5 on graph. I got the solution from http://stackoverflow.com/questions/1805687/number-of-weeks-in-month , where I can pass first date and lasts dates of month and the date component will provide me weeks of month – V.V Jan 13 '12 at 02:04

1 Answers1

0

For iPhone See: Number of weeks in month For web/Javascript, See: Get Weeks In Month Through Javascript. Algorithm is simple (from above page):

function weekCount(year, month_number) {

    // month_number is in the range 1..12

    var firstOfMonth = new Date(year, month_number-1, 1);
    var lastOfMonth = new Date(year, month_number, 0);

    var used = firstOfMonth.getDay() + lastOfMonth.getDate();

    return Math.ceil( used / 7);
}
Community
  • 1
  • 1
AriT
  • 18
  • 4
  • CEIL(number_of_days/7) will not give correct result. It is more then just deviding the number of days in a month by 7. Please read Weeks per month section here: http://en.wikipedia.org/wiki/ISO_week_date – Art Jan 30 '13 at 21:32