Meow
Meow

Reputation: 19071

How to find week of month for calendar which starts from Monday?

I have a calendar starting on Monday.

May, 2011

----------------------------
Mon Tue Wed Thu Fri Sat Sun
----------------------------
25  26  27  28  29  30   1     week 1
2   3   4    5   6   7   8     week 2
9   10  11  12  13  14   15    week 3
16  17  18  19  20  21   22    week 4
23  24  25  26  27  28   29    week 5
30  31  1   2    3  4     5    week 6

I want to find week of month on giving Date.

Below works fine on the calendar that starts on Sunday.

function getWeekOfMonth(date) {

  prefixes = ['1', '2', '3', '4', '5'];
  return prefixes[0 | date.getDate() / 7];
}

But in my Calendar (Monday based) if I choose May 1st, the return value if "1" thus gives me "week2" but it should be "week1".

If I choose May 15th, giving me "week4" instead of "week3".

It works on things like May 12th.

I tried to shift this one day gap (monday and sunday) and modified the algorithm which failed.

Could anyone show me how can I fix my algorithm correctly?

function getWeekOfMonth(date) {

  prefixes = ['1', '2', '3', '4', '5'];
  return prefixes[0 | date.getDate() - 1 / 7];
}

Upvotes: 2

Views: 8754

Answers (3)

dzimney
dzimney

Reputation: 552

function getWeekOfMonth(date) {

    var nth = 0; // returning variable.
    var timestamp = date.getTime(); // get UTC timestamp of date.
    var month = date.getMonth(); // get current month.
    var m = month; // save temp value of month.

    while( m == month ) {  // check if m equals our date's month.
      nth++; // increment our week count.
      // update m to reflect previous week (previous to last value of m).
      m = new Date(timestamp - nth * 604800000).getMonth();
    }

    return nth;

}

Upvotes: 0

Pyae Phyo Aung
Pyae Phyo Aung

Reputation: 848

(When I tested the accepted answer in this post, found out that it does not work for months where first day of the month is Monday or Tuesday. But my answer is mainly based on the accepted answer also.)

I think this function should do the work.

        function getWeekNo(date) {

            var day = date.getDate()

            //get weekend date
            day += (date.getDay() == 0 ? 0 : 7 - date.getDay());

            return Math.ceil(parseFloat(day) / 7);
        }

        alert(getWeekNo(new Date(2015, 2, 31)));

When I tested, it turns out giving correct results for:

  1. Months where first day of the month is Monday, Tuesday or Sunday
  2. Months that span over four, five and six weeks

Upvotes: 2

ratchet freak
ratchet freak

Reputation: 48196

you first need to check on what day the month starts then add k so that date.getDate() + k is 7 for the first monday of the month

then it's pretty easy

function getWeekOfMonth(date) {
  var adjustedDate = date.getDate()+k;
  prefixes = ['0', '1', '2', '3', '4', '5'];
  return prefixes[0 | adjustedDate / 7];
}

edit:

you can use getDay to find the current day of week and use it in relation to getDate to find the monday of the current week

function getWeekOfMonth(date) {
  var day = date.getDate()
  day-=(date.getDay()==0?6:date.getDay()-1);//get monday of this week
  //special case handling for 0 (sunday)

  day+=7;
  //for the first non full week the value was negative

  prefixes = ['0', '1', '2', '3', '4', '5'];
  return prefixes[0 | (day) / 7];
}

Upvotes: 7

Related Questions