Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get week of the month

How can i get the week number of month using javascript / jquery?

For ex.:

First Week: 5th July, 2010. / Week Number = First monday

Previous Week: 12th July, 2010. / Week Number = Second monday

Current Date: 19th July, 2010. / Week Number = Third Monday

Next week: 26th July, 2010. / Week Number = Last monday

like image 708
Prasad Avatar asked Jul 19 '10 10:07

Prasad


People also ask

How do you find the week of the 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.

How do you get the week of the month in Excel?

=INT((DAY(B5)-1)/7)+1 Here, first using the DAY function we get the value of a day of the date in Cell B5. Then, we subtracted the value by 1 and divided it by 7 to get the day into the week number of the month.

What is week 1 of the month?

First week starts when any day of a given month falls in that week: July Week 1 from 6/25 to 7/1. July Week 2 from 7/2 to 7/8.

How does the Weeknum formula work in Excel?

The WEEKNUM Function[1] is an Excel DATE and TIME Function. It will return the week number of a specific date. The function will return an integer that represents a week number from 1 to 52 weeks of the year.


1 Answers

This is an old question, here is my cross-browser solution based on:

  1. Weeks start on Sunday
  2. The first week of a month is the one that contains the first of the month

So in March 2013:

  • Fri 1 Mar is the first day of week 1
  • Sun 3 Mar is the start of week 2
  • Sun 31 Mar is the start of week 6 (and is the only day in the that week)
  • Mon 1 Apr is the first day of week 1 in April.

    Date.prototype.getWeekOfMonth = function(exact) {         var month = this.getMonth()             , year = this.getFullYear()             , firstWeekday = new Date(year, month, 1).getDay()             , lastDateOfMonth = new Date(year, month + 1, 0).getDate()             , offsetDate = this.getDate() + firstWeekday - 1             , index = 1 // start index at 0 or 1, your choice             , weeksInMonth = index + Math.ceil((lastDateOfMonth + firstWeekday - 7) / 7)             , week = index + Math.floor(offsetDate / 7)         ;         if (exact || week < 2 + index) return week;         return week === weeksInMonth ? index + 5 : week;     };          // Simple helper to parse YYYY-MM-DD as local     function parseISOAsLocal(s){       var b = s.split(/\D/);       return new Date(b[0],b[1]-1,b[2]);     }      // Tests     console.log('Date          Exact|expected   not exact|expected');     [   ['2013-02-01', 1, 1],['2013-02-05', 2, 2],['2013-02-14', 3, 3],         ['2013-02-23', 4, 4],['2013-02-24', 5, 6],['2013-02-28', 5, 6],         ['2013-03-01', 1, 1],['2013-03-02', 1, 1],['2013-03-03', 2, 2],         ['2013-03-15', 3, 3],['2013-03-17', 4, 4],['2013-03-23', 4, 4],         ['2013-03-24', 5, 5],['2013-03-30', 5, 5],['2013-03-31', 6, 6],         ['2013-04-01', 1, 1]     ].forEach(function(test){       var d = parseISOAsLocal(test[0])       console.log(test[0] + '        ' +        d.getWeekOfMonth(true) + '|' + test[1] + '                  ' +       d.getWeekOfMonth() + '|' + test[2]);      });

You don't need to put it directly on the prototype if you don't want to. In my implementation, 6 means "Last", not "Sixth". If you want it to always return the actual week of the month, just pass true.

EDIT: Fixed this to handle 5 & 6-week months. My "unit tests", feel free to fork: http://jsfiddle.net/OlsonDev/5mXF6/1/.

like image 84
Olson.dev Avatar answered Oct 05 '22 19:10

Olson.dev