Get Weeks In Month Through Javascript

后端 未结 17 1261
盖世英雄少女心
盖世英雄少女心 2020-11-30 09:03

In Javascript, how do I get the number of weeks in a month? I can\'t seem to find code for this anywhere.

I need this to be able to know how many rows I need for a g

相关标签:
17条回答
  • 2020-11-30 09:25
    function weeksinMonth(m, y){
     y= y || new Date().getFullYear();
     var d= new Date(y, m, 0);
     return Math.floor((d.getDate()- 1)/7)+ 1;     
    }
    alert(weeksinMonth(3))
    

    // the month range for this method is 1 (january)-12(december)

    0 讨论(0)
  • 2020-11-30 09:27

    The most easy to understand way is

    <div id="demo"></div>
    
    <script type="text/javascript">
    
     function numberOfDays(year, month)
     {
       var d = new Date(year, month, 0);
       return d.getDate();
     }
    
    
     function getMonthWeeks(year, month_number)
     {
       var $num_of_days       = numberOfDays(year, month_number)
        ,  $num_of_weeks      = 0
        ,  $start_day_of_week = 0; 
    
       for(i=1; i<=$num_of_days; i++)
       {
          var $day_of_week = new Date(year, month_number, i).getDay();
          if($day_of_week==$start_day_of_week)
          {
            $num_of_weeks++;
          }   
       }
    
        return $num_of_weeks;
     }
    
       var d = new Date()
          , m = d.getMonth()
          , y = d.getFullYear();
    
       document.getElementById('demo').innerHTML = getMonthWeeks(y, m);
    </script>
    
    0 讨论(0)
  • 2020-11-30 09:29
    function getWeeksInMonth(month_number, year) {
      console.log("year - "+year+" month - "+month_number+1);
    
      var day = 0;
      var firstOfMonth = new Date(year, month_number, 1);
      var lastOfMonth = new Date(year, parseInt(month_number)+1, 0);
    
      if (firstOfMonth.getDay() == 0) {
        day = 2;
        firstOfMonth = firstOfMonth.setDate(day);
        firstOfMonth = new Date(firstOfMonth);
      } else if (firstOfMonth.getDay() != 1) {
        day = 9-(firstOfMonth.getDay());
        firstOfMonth = firstOfMonth.setDate(day);
        firstOfMonth = new Date(firstOfMonth);
      }
    
      var days = (lastOfMonth.getDate() - firstOfMonth.getDate())+1
      return Math.ceil( days / 7);              
    }
    

    It worked for me. Please try

    Thanks all

    0 讨论(0)
  • 2020-11-30 09:31

    This works for me,

    function(d){
        var firstDay = new Date(this.getFullYear(), this.getMonth(), 1).getDay();
        return Math.ceil((d.getDate() + (firstDay - 1))/7);
    }
    

    "d" should be the date.

    0 讨论(0)
  • 2020-11-30 09:35

    ES6 variant, using consistent zero-based months index. Tested for years from 2015 to 2025.

    /**
     * Returns number of weeks
     *
     * @param {Number} year - full year (2018)
     * @param {Number} month - zero-based month index (0-11)
     * @param {Boolean} fromMonday - false if weeks start from Sunday, true - from Monday.
     * @returns {number}
     */
    const weeksInMonth = (year, month, fromMonday = false) => {
        const first = new Date(year, month, 1);
        const last  = new Date(year, month + 1, 0);
        let dayOfWeek = first.getDay();
        if (fromMonday && dayOfWeek === 0) dayOfWeek = 7;
        let days = dayOfWeek + last.getDate();
        if (fromMonday) days -= 1;
        return Math.ceil(days / 7);
    }
    0 讨论(0)
  • 2020-11-30 09:36

    You'll have to calculate it.

    You can do something like

    var firstDay = new Date(2010, 0, 1).getDay(); // get the weekday january starts on
    var numWeeks = 5 + (firstDay >= 5 ? 1 : 0); // if the months starts on friday, then it will end on sunday
    

    Now we just need to genericize it.

    var dayThreshold = [ 5, 1, 5, 6, 5, 6, 5, 5, 6, 5, 6, 5 ];
    function GetNumWeeks(month, year)
    {
        var firstDay = new Date(year, month, 1).getDay();
        var baseWeeks = (month == 1 ? 4 : 5); // only February can fit in 4 weeks
        // TODO: account for leap years
        return baseWeeks + (firstDay >= dayThreshold[month] ? 1 : 0); // add an extra week if the month starts beyond the threshold day.
    }
    

    Note: When calling, remember that months are zero indexed in javascript (i.e. January == 0).

    0 讨论(0)
提交回复
热议问题