0

For Ex: November 2013 has Week number 45 to 49 and December 2014 has Week number 49 to 53

Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57
  • 3
    It's not the same everywhere. In Germany for example week number 1 is the week (starting Monday and ending Sunday) that contains January 4. You could also say: Week number 1 is the first week (starting Monday) with minimum of 4 January days in it – devnull69 Nov 05 '13 at 15:37
  • possible duplicate of [Show week number with Javascript?](http://stackoverflow.com/questions/7765767/show-week-number-with-javascript) – mayabelle Nov 05 '13 at 15:39
  • this is not hard to research in wed search – charlietfl Nov 05 '13 at 15:46

1 Answers1

0

This is a solution for the German/European/ISO week.

  1. Find the Thursday in the week of the given date. The Thursday determines the year to which the requested week belongs
  2. Find the Thursday in the week of January 4 of that year. This Thursday is in week 1
  3. Calculate the week difference between current week's Thursday and week one's Thursday plus one ... which will be the week number of the given date

Try this

function thursday(mydate) {
  var Th=new Date();
  Th.setTime(mydate.getTime() + (3-((mydate.getDay()+6) % 7)) * 86400000);
  return Th;
}

function getCalWeek(y, m, d) {
    thedate=new Date(y, m-1, d);
    ThursDate=thursday(thedate);
    weekYear=ThursDate.getFullYear();
    ThursWeek1=thursday(new Date(weekYear,0,4));
    theweek=Math.floor(1.5+(ThursDate.getTime()-ThursWeek1.getTime())/86400000/7);
    return theweek;
}

console.log("The week of the given date is: " + getCalWeek(2013, 11, 5));

EDIT: For your specific question, you need to give the month and the year, then you calculate the weeks

function daysInMonth(month,year) {
    var m = [31,28,31,30,31,30,31,31,30,31,30,31];
    if (month != 2) return m[month - 1];
    if (year%4 != 0) return m[1];
    if (year%100 == 0 && year%400 != 0) return m[1];
    return m[1] + 1;
}

function getWeekRange(m, y) {
    var startWeek = getCalWeek(y, m, 1);
    var endWeek = getCalWeek(y, m, daysInMonth(m, y));
    return startWeek + " to " + endWeek;
}
devnull69
  • 16,402
  • 8
  • 50
  • 61