Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get weeks in year

Moment js has a function to get the number of days in a month : http://momentjs.com/docs/#/displaying/days-in-month/

However I could not find a function to find the number of iso weeks in a year (52 or 53).

like image 315
basarat Avatar asked Aug 28 '13 03:08

basarat


People also ask

How do you calculate weeks in a year?

Divide the number of days in a year (365) by the days there are in a week (7): A year has on average 52.143 weeks = 52 weeks plus one day. Every four years we have a leap year, which has 366 days. A leap year, therefore, has 52 weeks and 2 days.

Are there always 52 weeks in a year?

A note about leap years Well, in a leap year, you actually have two additional days above the standard 52 weeks. To be precise again, a calendar leap year has 52.286 weeks. That's 0.143 fewer weeks in a regular year than in a leap year. Speaking of which, when exactly is the next leap year?

Can there be 53 weeks in a year?

Which Years Have 53 Weeks? The weeks of the year in a Gregorian calendar are numbered from week 1 to week 52 or 53, depending on several varying factors. Most years have 52 weeks, but if the year starts on a Thursday or is a leap year that starts on a Wednesday, that particular year will have 53 numbered weeks.

How often are there 53 weeks in a year?

This occurs approximately every five to six years, though this is not always the case. 2006, 2012, 2017 and 2023 are all 53-week years.


2 Answers

Here's an answer that isn't dependent on a library. It uses a function to calculate the week in the year that 31 December falls in for the required year. If the week is 1 (i.e. 31 December is in the first week of the following year), it moves the day number lower until it gets a different value, which will be the last week of the required year.

function getWeekNumber(d) {
  // Copy date so don't modify original
  d = new Date(+d);
  d.setHours(0, 0, 0, 0);
  // Set to nearest Thursday: current date + 4 - current day number
  // Make Sunday's day number 7
  d.setDate(d.getDate() + 4 - (d.getDay() || 7));
  // Get first day of year
  var yearStart = new Date(d.getFullYear(), 0, 1);
  // Calculate full weeks to nearest Thursday
  var weekNo = Math.ceil((((d - yearStart) / 86400000) + 1) / 7)
  // Return array of year and week number
  return [d.getFullYear(), weekNo];
}

function weeksInYear(year) {
  var month = 11,
    day = 31,
    week;

  // Find week that 31 Dec is in. If is first week, reduce date until
  // get previous week.
  do {
    d = new Date(year, month, day--);
    week = getWeekNumber(d)[1];
  } while (week == 1);

  return week;
}

[2015, 2016, 2029, new Date().getFullYear()].forEach(year =>
  console.log(`${year} has ${weeksInYear(year)} weeks`)
);

The getWeekNumber code is from here: Get week of year in JavaScript like in PHP.

Edit

Alternatively, if 31 December is in week 1 of the following year, then the subject year has 52 weeks and otherwise has 53 weeks.

function getWeekNumber(d) {
  d = new Date(+d);
  d.setHours(0, 0, 0, 0);
  d.setDate(d.getDate() + 4 - (d.getDay() || 7));
  var yearStart = new Date(d.getFullYear(), 0, 1);
  var weekNo = Math.ceil((((d - yearStart) / 86400000) + 1) / 7)
  return [d.getFullYear(), weekNo];
}

function weeksInYear(year) {
  var d = new Date(year, 11, 31);
  var week = getWeekNumber(d)[1];
  return week == 1 ? 52 : week;
}

[2015, 2016, 2029, new Date().getFullYear()].forEach(year =>
  console.log(`${year} has ${weeksInYear(year)} weeks`)
);
like image 123
RobG Avatar answered Sep 23 '22 21:09

RobG


Use isoWeek on the last day of the year to get the number of weeks e.g. :

function weeksInYear(year) {
   return Math.max(
            moment(new Date(year, 11, 31)).isoWeek()
          , moment(new Date(year, 11, 31-7)).isoWeek()
   );
}
like image 42
basarat Avatar answered Sep 21 '22 21:09

basarat