通过Javascript获得几个月的周

Ste*_*ins 16 javascript calendar

在Javascript中,如何获得一个月内的周数?我似乎无法在任何地方找到代码.

我需要这个能够知道给定月份需要多少行.

更具体地说,我希望一周中至少有一天的周数(一周定义为星期日开始,星期六结束).

所以,对于这样的事情,我想知道它有5个星期:

S  M  T  W  R  F  S

         1  2  3  4

5  6  7  8  9  10 11

12 13 14 15 16 17 18

19 20 21 22 23 24 25

26 27 28 29 30 31 
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

Ed *_*oor 27

周从周日开始

即使二月没有在周日开始,这也应该有效.

function weekCount(year, month_number) {

    // month_number is in the range 1..12

    var firstOfMonth = new Date(year, month_number-1, 1);
    var lastOfMonth = new Date(year, month_number, 0);

    var used = firstOfMonth.getDay() + lastOfMonth.getDate();

    return Math.ceil( used / 7);
}
Run Code Online (Sandbox Code Playgroud)

周从星期一开始

function weekCount(year, month_number) {

    // month_number is in the range 1..12

    var firstOfMonth = new Date(year, month_number-1, 1);
    var lastOfMonth = new Date(year, month_number, 0);

    var used = firstOfMonth.getDay() + 6 + lastOfMonth.getDate();

    return Math.ceil( used / 7);
}
Run Code Online (Sandbox Code Playgroud)

几周开始另一天

function weekCount(year, month_number, startDayOfWeek) {
  // month_number is in the range 1..12

  // Get the first day of week week day (0: Sunday, 1: Monday, ...)
  var firstDayOfWeek = startDayOfWeek || 0;

  var firstOfMonth = new Date(year, month_number-1, 1);
  var lastOfMonth = new Date(year, month_number, 0);
  var numberOfDaysInMonth = lastOfMonth.getDate();
  var firstWeekDay = (firstOfMonth.getDay() - firstDayOfWeek + 7) % 7;

  var used = firstWeekDay + numberOfDaysInMonth;

  return Math.ceil( used / 7);
}
Run Code Online (Sandbox Code Playgroud)

  • 如果星期一星期一开始不工作;) (4认同)
  • 将此代码用于jQuery日历插件.感谢分享!你可以在这里查看:https://github.com/joelalejandro/jquery-ja/wiki/ja.Calendar (2认同)
  • 从星期一开始时仍然无法工作。只需检查 2018 年 1 月。它应该返回 5 周,但它返回 6。 (2认同)
  • 的确。2017 年 12 月返回 6 周,而当时只有 5 周。 (2认同)

imd*_*sen 6

这是非常简单的两行代码。我测试过100%。

Date.prototype.getWeekOfMonth = function () {
    var firstDay = new Date(this.setDate(1)).getDay();
    var totalDays = new Date(this.getFullYear(), this.getMonth() + 1, 0).getDate();
    return Math.ceil((firstDay + totalDays) / 7);
}
Run Code Online (Sandbox Code Playgroud)

如何使用

var totalWeeks = new Date().getWeekOfMonth();
console.log('Total Weeks in the Month are : + totalWeeks ); 
Run Code Online (Sandbox Code Playgroud)


小智 6

这里提出的解决方案中没有人不能正常工作,所以我编写了自己的变体,它适用于任何情况.

简单而有效的解决方案

/**
 * Returns count of weeks for year and month
 *
 * @param {Number} year - full year (2016)
 * @param {Number} month_number - month_number is in the range 1..12
 * @returns {number}
 */
var weeksCount = function(year, month_number) {
    var firstOfMonth = new Date(year, month_number - 1, 1);
    var day = firstOfMonth.getDay() || 6;
    day = day === 1 ? 0 : day;
    if (day) { day-- }
    var diff = 7 - day;
    var lastOfMonth = new Date(year, month_number, 0);
    var lastDate = lastOfMonth.getDate();
    if (lastOfMonth.getDay() === 1) {
        diff--;
    }
    var result = Math.ceil((lastDate - diff) / 7);
    return result + 1;
};
Run Code Online (Sandbox Code Playgroud)

你可以在这里试试