Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Weeks In Month Through Javascript

Tags:

In Javascript, how do I get the number of weeks in a month? I can't seem to find code for this anywhere.

I need this to be able to know how many rows I need for a given month.

To be more specific, I would like the number of weeks that have at least one day in the week (a week being defined as starting on Sunday and ending on Saturday).

So, for something like this, I would want to know it has 5 weeks:

S  M  T  W  R  F  S           1  2  3  4  5  6  7  8  9  10 11  12 13 14 15 16 17 18  19 20 21 22 23 24 25  26 27 28 29 30 31  

Thanks for all the help.

like image 384
Stephen Watkins Avatar asked Mar 20 '10 16:03

Stephen Watkins


People also ask

How do you calculate week of month?

We can easily count the number of weeks in a month by first counting the number of days in the month. Then, we divide the number of days by 7 since 1 week has 7 days. For example, the month of January has 31 days, so the number of weeks in January would be: 31/7 = 4 weeks + 3 days.


2 Answers

Weeks start on Sunday

This ought to work even when February doesn't start on Sunday.

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); } 

Weeks start on Monday

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() + 6 + lastOfMonth.getDate();      return Math.ceil( used / 7); } 

Weeks start another day

function weekCount(year, month_number, startDayOfWeek) {   // month_number is in the range 1..12    // Get the first day of week week day (0: Sunday, 1: Monday, ...)   var firstDayOfWeek = startDayOfWeek || 0;    var firstOfMonth = new Date(year, month_number-1, 1);   var lastOfMonth = new Date(year, month_number, 0);   var numberOfDaysInMonth = lastOfMonth.getDate();   var firstWeekDay = (firstOfMonth.getDay() - firstDayOfWeek + 7) % 7;    var used = firstWeekDay + numberOfDaysInMonth;    return Math.ceil( used / 7); } 
like image 67
Ed Poor Avatar answered Sep 20 '22 14:09

Ed Poor


None of the solutions proposed here don't works correctly, so I wrote my own variant and it works for any cases.

Simple and working solution:

  
/**  * Returns count of weeks for year and month  *  * @param {Number} year - full year (2016)  * @param {Number} month_number - month_number is in the range 1..12  * @returns {number}  */ var weeksCount = function(year, month_number) {     var firstOfMonth = new Date(year, month_number - 1, 1);     var day = firstOfMonth.getDay() || 6;     day = day === 1 ? 0 : day;     if (day) { day-- }     var diff = 7 - day;     var lastOfMonth = new Date(year, month_number, 0);     var lastDate = lastOfMonth.getDate();     if (lastOfMonth.getDay() === 1) {         diff--;     }     var result = Math.ceil((lastDate - diff) / 7);     return result + 1; }; 

you can try it here
like image 39
santakllouse Avatar answered Sep 18 '22 14:09

santakllouse