当前位置: 代码迷 >> Web前端 >> 依据年、周获得开始日期与结束日期
  详细解决方案

依据年、周获得开始日期与结束日期

热度:151   发布时间:2012-11-23 00:03:29.0
根据年、周获得开始日期与结束日期
//根据年、周获取开始日期与结束日期
function getDateByYearAndWeekNo(year,week){
	var d = new Date(year, 0, 1);
	d.setDate(d.getDay()==1?((week)   *   7):(parseInt("1065432".charAt(d.getDay()))   +   (week-1)   *   7));   
    var fe = getFirstAndEnd(d); 
    document.getElementById("startDate").value   =   fe.first.format("yyyy年MM月dd日");   
    document.getElementById("endDate").value   =   fe.end.format("yyyy年MM月dd日"); 
}

  Date.prototype.getWeek   =   function(flag)   
  {   
      var   first   =   new   Date(this.getFullYear(),   0,   1);   
      var   n   =   parseInt("1065432".charAt(first.getDay()));   
      n   =   this.getTime()-first.getTime()-n*24*60*60*1000;   
      n   =   Math.ceil(n/(7*24*60*60*1000));   
      return   (flag==true&&first.getDay()!=1)?(n+1):n;   
  };   
  Date.prototype.format   =   function(format)   
  {   
      var   o   =   {   
          "M+"   :   this.getMonth()+1,   //month   
          "d+"   :   this.getDate(),         //day   
          "h+"   :   this.getHours(),       //hour   
          "m+"   :   this.getMinutes(),   //minute   
          "s+"   :   this.getSeconds(),   //second   
          "q+"   :   Math.floor((this.getMonth()+3)/3),     //quarter   
          "S"   :   this.getMilliseconds()   //millisecond   
      }   
      if(/(y+)/.test(format))   format=format.replace(RegExp.$1,   
          (this.getFullYear()+"").substr(4   -   RegExp.$1.length));   
      for(var   k   in   o)if(new   RegExp("("+   k   +")").test(format))   
          format   =   format.replace(RegExp.$1,   
              RegExp.$1.length==1   ?   o[k]   :     
                  ("00"+   o[k]).substr((""+   o[k]).length));   
      return   format;   
  };   
    
  function   getFirstAndEnd(d)   
  {   
      var   w   =   d.getDay(),   n   =   24*60*60*1000;   
      var   first   =   new   Date(d.getTime()   -   parseInt("6012345".charAt(w))*n-n);//一周开始为周日,如果为周一就不用减n了   
      var   end   =   new   Date(d.getTime()   +   parseInt("0654321".charAt(w))*n-n);   
      return   {first:   first,   end:   end};   
  }   
  相关解决方案