日期:2014-05-16 浏览次数:20404 次
<script> function TimeSpan(month,start,end){ this.month=month; this.start=start; this.end=end; this.toString=function(){ return this.month+"月,所在周数为:"+this.start+"-"+this.end; } } function get(month){ var date,month_end; if(month==get.startDay.getMonth()+1){ date=new Date(get.startDay); } if(month<get.startDay.getMonth()+1){ date=new Date(get.startDay); date.setMonth(get.startDay.getMonth()+month); date.setDate(1); }else{ date=new Date(get.startDay); date.setMonth(month-1); date.setDate(1); } month_end=new Date(date); month_end.setMonth(month_end.getMonth()+1); month_end.setDate(1); var a=date-get.startDay; var b=month_end-get.startDay; var spanA=a/1000/60/60/24/7; var spanB=b/1000/60/60/24/7; var ts=new TimeSpan(); ts.month=date.getMonth()+1; ts.start=parseInt(spanA)+(spanA%1>0?1:0); ts.end=parseInt(spanB)+(spanB%1>0?1:0); if(month==get.startDay.getMonth()+1)ts.start++; return ts; } get.startDay=new Date(2007,11,3);//11其实就是指12月 get.endDay=new Date(2008,5,30);//5其实是指6月 /* 测试输出 */ document.write(get(12),"<br />"); for(var i=1;i<=6;i++){ document.write(get(i),"<br />"); } </script>
------解决方案--------------------
有bug,多加几行
<script> function TimeSpan(month,start,end){ this.month=month; this.start=start; this.end=end; this.toString=function(){ return this.month+"月,所在周数为:"+this.start+"-"+this.end; } } function get(month){ var date,month_end; if(month==get.startDay.getMonth()+1){ date=new Date(get.startDay); } if(month<get.startDay.getMonth()+1){ date=new Date(get.startDay); date.setMonth(get.startDay.getMonth()+month); date.setDate(1); }else{ date=new Date(get.startDay); date.setMonth(month-1); date.setDate(1); } month_end=new Date(date); month_end.setMonth(month_end.getMonth()+1); month_end.setDate(1); /* 多了这几行 必须的*/ if(date.getMonth()==get.endDay.getMonth()){ month_end=new Date(get.endDay); month_end.setDate(month_end.getDate()+1); } var a=date-get.startDay; var b=month_end-get.startDay; var spanA=a/1000/60/60/24/7; var spanB=b/1000/60/60/24/7; var ts=new TimeSpan(); ts.month=date.getMonth()+1; ts.start=parseInt(spanA)+(spanA%1>0?1:0); ts.end=parseInt(spanB)+(spanB%1>0?1:0); if(month==get.startDay.getMonth()+1)ts.start++; return ts; } get.startDay=new Date(2007,11,3);//11其实就是指12月 get.endDay=new Date(2008,5,30);//5其实是指6月 /* 测试输出 */ document.write(get(12),"<br />"); for(var i=1;i<=6;i++){ document.write(get(i),"<br />"); } </script>
------解决方案--------------------
lz多修改一下开始个结束的2个期限。。多试下有没有错~~~
------解决方案--------------------
参考及效果演示http://www.515161.cn/515161/admins/js/date.html