Get week of the month

前端 未结 14 1664
野趣味
野趣味 2020-11-30 08:55

How can i get the week number of month using javascript / jquery?

For ex.:

First Week: 5th July, 2010. / Week Number = First monday

相关标签:
14条回答
  • 2020-11-30 09:26
    week_number = 0 | new Date().getDate() / 7
    
    0 讨论(0)
  • 2020-11-30 09:27

    Having struggle with this topic too - thanks to Olson.dev! I've shortened his function a little bit, if somebody is interessted:

    // returns week of the month starting with 0
    Date.prototype.getWeekOfMonth = function() {
      var firstWeekday = new Date(this.getFullYear(), this.getMonth(), 1).getDay();
      var offsetDate = this.getDate() + firstWeekday - 1;
      return Math.floor(offsetDate / 7);
    }
    

    update: if you need a localized version - you have to tweak the firstWeekday variable

    // german version - week starts with monday
    Date.prototype.getWeekOfMonth = function() {
      var firstWeekday = new Date(this.getFullYear(), this.getMonth(), 1).getDay() - 1;
      if (firstWeekday < 0) firstWeekday = 6;
      var offsetDate = this.getDate() + firstWeekday - 1;
      return Math.floor(offsetDate / 7);
    }
    
    0 讨论(0)
  • 2020-11-30 09:28

    Please try below function.This one is considering week start date as Monday and week end date as Sunday.

    getWeekNumber(date) {
        var monthStartDate =new Date(new Date().getFullYear(), new 
             Date().getMonth(), 1);
        monthStartDate = new Date(monthStartDate);
        var day = startdate.getDay();
        date = new Date(date);
        var date = date.getDate();
        return Math.ceil((date+ day-1)/ 7);
    }
    
    0 讨论(0)
  • 2020-11-30 09:29

    This is a few years on, but I've needed to use this functionality recently and, for certain dates in years 2016/2020 (such as January 31st), none of the code here works.

    It's not the most efficient by any means, but hopefully this helps someone out as it's the only thing I could get working for those years along with every other year.

    Date.prototype.getWeekOfMonth = function () {
        var dayOfMonth = this.getDay();
        var month = this.getMonth();
        var year = this.getFullYear();
        var checkDate = new Date(year, month, this.getDate());
        var checkDateTime = checkDate.getTime();
        var currentWeek = 0;
    
        for (var i = 1; i < 32; i++) {
            var loopDate = new Date(year, month, i);
    
            if (loopDate.getDay() == dayOfMonth) {
                currentWeek++;
            }
    
            if (loopDate.getTime() == checkDateTime) {
                return currentWeek;
            }
        }
    };
    
    0 讨论(0)
  • 2020-11-30 09:31
    function getWeekOfMonth(date) {
      const startWeekDayIndex = 1; // 1 MonthDay 0 Sundays
      const firstDate = new Date(date.getFullYear(), date.getMonth(), 1);
      const firstDay = firstDate.getDay();
    
      let weekNumber = Math.ceil((date.getDate() + firstDay) / 7);
      if (startWeekDayIndex === 1) {
        if (date.getDay() === 0 && date.getDate() > 1) {
          weekNumber -= 1;
        }
    
        if (firstDate.getDate() === 1 && firstDay === 0 && date.getDate() > 1) {
          weekNumber += 1;
        }
      }
      return weekNumber;
    }
    

    I hope this works Tested until 2025

    0 讨论(0)
  • 2020-11-30 09:31
    function getWeekOfMonth(date) {
    
      var nth = 0; // returning variable.
      var timestamp = date.getTime(); // get UTC timestamp of date.
      var month = date.getMonth(); // get current month.
      var m = month; // save temp value of month.
    
      while( m == month ) {  // check if m equals our date's month.
        nth++; // increment our week count.
        // update m to reflect previous week (previous to last value of m).
        m = new Date(timestamp - nth * 604800000).getMonth();
      }
    
      return nth;
    
    }
    
    0 讨论(0)
提交回复
热议问题