js实现获取当前周,过去和未来周的时间段日期

function getWeekTime(){
        var currentFirstDate;
        var formatDate = function(date){       
          var year = date.getFullYear()+'年';
          var month = (date.getMonth()+1)+'月';
          var day = date.getDate()+'日';
          var week = '('+['星期天','星期一','星期二','星期三','星期四','星期五','星期六'][date.getDay()]+')'; 
  
          return year+month+day+'  '+week;
        };
  
        var addDate= function(date,n){    
          date.setDate(date.getDate()+n);    
          return date;
        };
  
        var setDate = function(date){       
          var week = date.getDay()-1;
          date = addDate(date,week*-1);
          currentFirstDate = new Date(date);

          var arr = [];
          for(var i = 0;i < 7;i++){         
            var a = formatDate(i==0 ? date : addDate(date,1));   //星期一开始
            arr.push(a)
          }   
          return arr;     
        };
        setDate(new Date())
        var num = 0;//num为0时表示当前周,递增或递减时周数也递增或递减
        return setDate(addDate(currentFirstDate,7*num));
    }

 

你可能感兴趣的:(JavaScript)