Get week of the month

前端 未结 14 1784
野趣味
野趣味 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: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;
    
    }
    

提交回复
热议问题