Get week of the month

Viewed 75345

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

17 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/.

function getWeekOfMonth(date) {
  const startWeekDayIndex = 1; // 1 MonthDay 0 Sundays
  const firstDate = new Date(date.getFullYear(), date.getMonth(), 1);
  const firstDay = firstDate.getDay();

  let weekNumber = Math.ceil((date.getDate() + firstDay) / 7);
  if (startWeekDayIndex === 1) {
    if (date.getDay() === 0 && date.getDate() > 1) {
      weekNumber -= 1;
    }

    if (firstDate.getDate() === 1 && firstDay === 0 && date.getDate() > 1) {
      weekNumber += 1;
    }
  }
  return weekNumber;
}

I hope this works Tested until 2025

None of the previous solutions included the "Last" on the response and I was able to do it by checking the next occurrence of the same day of the week and checking if it's still on this month.

The accepted answer fails in many cases (I tested it with today's date - 2021-05-14 - and it returned "Third Friday" when it's actually the second).

This code below was tested even with April 2017 (a rare case of a month with 6 weeks).

April 2017 - month with 6 weeks

/**
 * Get the week number of the month, from "First" to "Last"
 * @param {Date} date 
 * @returns {string}
 */
 function weekOfTheMonth(date) {
  const day = date.getDate()
  const weekDay = date.getDay()
  let week = Math.ceil(day / 7)
  
  const ordinal = ['First', 'Second', 'Third', 'Fourth', 'Last']
  const weekDays  = ['Sunday','Monday','Tuesday','Wednesday', 'Thursday','Friday','Saturday']
  

  // Check the next day of the week and if it' on the same month, if not, respond with "Last"
  const nextWeekDay = new Date(date.getTime() + (1000 * 60 * 60 * 24 * 7))
  if (nextWeekDay.getMonth() !== date.getMonth()) {
    week = 5
  }
  
  return `${ordinal[week - 1]} ${weekDays[weekDay]}`
}

const days = [
  new Date('2021-05-14'),
  new Date('26 July 2010'),
  new Date('5 July 2010'),
  new Date('12 July 2010'),
  new Date('22 April 2017'),
  new Date('29 April 2017'),
]

for (let i = 0; i < days.length; i += 1) {
  const d = days[i]
  console.log(d, weekOfTheMonth(d))
}

Please try below function.This one is considering week start date as Monday and week end date as Sunday.

getWeekNumber(date) {
    var monthStartDate =new Date(new Date().getFullYear(), new 
         Date().getMonth(), 1);
    monthStartDate = new Date(monthStartDate);
    var day = startdate.getDay();
    date = new Date(date);
    var date = date.getDate();
    return Math.ceil((date+ day-1)/ 7);
}

You just need this:

Math.ceil(new Date().getDate() / 7)
Related