others - 日历 - 通过Javascript获取一个月有几周

在Javascript中,如何获取一个月有几周? 我似乎在哪儿也找不到这个代码。

更具体地说,我想知道某一天在哪一周的周数(一周被定义为从星期日开始到星期六结束)。

 

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 



感谢你的帮助。

时间:

周从周日开始

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

 

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


}



周从星期一开始

 

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


}



周开始另一天

 

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


}



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

简单和有效的解决方案:

 

/**


 * 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;


};




你可以试试这里

你得计算一下。

你可以这样做

 

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



现在我们只需要对它进行泛化。

 

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.


}



调用时,请记住,在javascript中月份是零索引(即, 一月 == 0 )。

 

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



//此方法的月范围为1(一月)-12(十二月),

最容易理解的方法是

 

<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>



使用 moment js

 

function getWeeksInMonth(year, month){



 var monthStart = moment().year(year).month(month).date(1);


 var monthEnd = moment().year(year).month(month).endOf('month');


 var numDaysInMonth = moment().year(year).month(month).endOf('month').date();



 //calculate weeks in given month


 var weeks = Math.ceil((numDaysInMonth + monthStart.day())/7);


 var weekRange = [];


 var weekStart = moment().year(year).month(month).date(1);


 var i=0;



 while(i<weeks){


 var weekEnd = moment(weekStart);



 if(weekEnd.endOf('week').date() <= numDaysInMonth && weekEnd.month() == month) {


 weekEnd = weekEnd.endOf('week').format('LL');


 }else{


 weekEnd = moment(monthEnd);


 weekEnd = weekEnd.format('LL')


 }



 weekRange.push({


 'weekStart': weekStart.format('LL'),


 'weekEnd': weekEnd


 });



 weekStart = weekStart.weekday(7);


 i++;


 }



 return weekRange;


 } console.log(getWeeksInMonth(2016, 7))



你可以使用我的time.js库 , 以下是weeksInMonth函数:

 

//http://github.com/augustl/time.js/blob/623e44e7a64fdaa3c908debdefaac1618a1ccde4/time.js#L67



weeksInMonth: function(){


 var millisecondsInThisMonth = this.clone().endOfMonth().epoch() - this.clone().firstDayInCalendarMonth().epoch();


 return Math.ceil(millisecondsInThisMonth/MILLISECONDS_IN_WEEK);


},



这可能有点模糊,因为功能的大小在endOfMonth和firstDayInCalendarMonth中,但是,你至少能够了解它的工作原理。

这是非常简单的两行代码, 我已经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);


}



如何使用

 

var totalWeeks = new Date().getWeekOfMonth();


console.log('Total Weeks in the Month are : + totalWeeks ); 



感谢Ed Poor的解决方案,这与Date原型相同。

 

Date.prototype.countWeeksOfMonth = function() {


 var year = this.getFullYear();


 var month_number = this.getMonth();


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


}



这样你就可以用它

 

var weeksInCurrentMonth = new Date().countWeeksOfMonth();


var weeksInDecember2012 = new Date(2012,12,1).countWeeksOfMonth();//6



 

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


}



请尝试

谢谢大家

...