标签:
参考资料:
周算法参考的是ISO8601定义的方法,如果您对此有疑问,请详见:http://en.wikipedia.org/wiki/ISO_week_date
JS代码实现:
var normalYear = [0,31,59,90,120,151,181,212,243,273,304,334];
var leapYear = [0,33,60,91,121,152,182,213,244,274,305,335];
var nowDate = new Date();
var dateStr = nowDate.Format(‘yyyy-MM-dd‘);
var year = parseInt(dateStr.substring(0,4));
var mon = parseInt(dateStr.substring(5,8));
var day = parseInt(dateStr.substring(8,10));
var numberOfWeek = parseInt(nowDate.getDay()); //
var orginDay = 0;
if(isLeapYear(year)){
orginDay = normalYear[numberOfWeek+1] + day;
}else{
orginDay = leapYear[numberOfWeek+1] +day ;
}
var weekNum = (orginDay - numberOfWeek +10)/7 ; //这个就是结果,这一天属于一年的哪一周
weekNum = weekNum.toString().substring(0,weekNum.toString().indexOf("."));
if(numberOfWeek == 0){
begTime = begTime.add(-(1000*60*60*24)*6).Format(‘yyyy-MM-dd‘);
endTime = endTime.Format(‘yyyy-MM-dd‘);
}else{
begTime = begTime.add(-((1000*60*60*24))*(numberOfWeek-1)).Format(‘yyyy-MM-dd‘);
endTime = endTime.add(((1000*60*60*24))*(7-numberOfWeek)).Format(‘yyyy-MM-dd‘);
}
//begTime、endTime 意思是一周中周一到周末的时间。
function isLeapYear(year){//判断是否闰年
if(((year%4)==0 && (year %100 !=0)) || (year % 400 == 0)){
return true;
}
return false;
}
标签:
原文地址:http://www.cnblogs.com/gambleAny/p/5436044.html