获取本周、上周、本月的日期,每周从星期一开始

var queryBeginDate,queryEndDate;
    function formatDate(date) { 
        var myyear = date.getFullYear(); 
        var mymonth = date.getMonth()+1; 
        var myweekday = date.getDate(); 
        if(mymonth < 10){ 
           mymonth = "0" + mymonth; 
        } 
        if(myweekday < 10){ 
           myweekday = "0" + myweekday; 
        } 
        return (myyear+"-"+mymonth + "-" + myweekday); 
    } 

    // 获取本周、上周、本月的日期,每周从星期一开始
    function getSelectDate(t){
      var now = new Date(); //当前日期 
      var nowDayOfWeek = now.getDay(); //今天本周的第几天 
      var nowDay = now.getDate(); //当前日 
      var nowMonth = now.getMonth(); //当前月 
      var nowYear = now.getFullYear(); //当前年 
      switch (t) {
       case "1"://本周
            queryBeginDate = formatDate(new Date(nowYear,nowMonth,nowDay-nowDayOfWeek+1));
            queryEndDate = formatDate(new Date(nowYear,nowMonth,nowDay-nowDayOfWeek+7));
           break;
       case "2"://上周
            queryBeginDate = formatDate( new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 6));
            queryEndDate = formatDate(new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 0));
           break;
       case "3"://本月
            queryBeginDate = formatDate(new Date(nowYear,nowMonth,1));
            queryEndDate = formatDate(new Date(new Date(nowYear,nowMonth+1,1)-1000*60*60*24));
           break;
       default:
           break;
      }
    }

你可能感兴趣的:(JavaScript)