javascript 获得一年中的周数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18478741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:04:52  来源:igfitidea点击:

Get weeks in year

javascriptmomentjs

提问by basarat

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

Moment js 有一个函数可以获取一个月中的天数: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).

但是我找不到一个函数来查找一年中的 iso 周数(52 或 53)。

回答by RobG

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.

这是一个不依赖于图书馆的答案。它使用一个函数来计算所需年份的 12 月 31 日所在的那一周。如果周为 1(即 12 月 31 日在下一年的第一周),它会将天数向下移动,直到获得不同的值,这将是所需年份的最后一周。

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 getWeekNumbercode is from here: Get week of year in JavaScript like in PHP.

getWeekNumber代码是从这里:获得今年的JavaScript的一周像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.

或者,如果 12 月 31 日在下一年的第 1 周,则该主题年有 52 周,否则有 53 周。

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`)
);

回答by basarat

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

在一年的最后一天使用 isoWeek 来获取周数,例如:

function weeksInYear(year) {
   return Math.max(
            moment(new Date(year, 11, 31)).isoWeek()
          , moment(new Date(year, 11, 31-7)).isoWeek()
   );
}

回答by S?ren H?yer Kristensen

Feb. 4th 2014the weeksInYear & isoWeeksInYear functions were added to moment.js

2014 年 2 月 4 日,weeksInYear 和 isoWeeksInYear 函数被添加到 moment.js

So today you can just use moment().isoWeeksInYear()or moment().weeksInYear()

所以今天你可以使用moment().isoWeeksInYear()moment().weeksInYear()

For more into see the docs

有关更多信息,请参阅文档