JS获取本周周一 周日日期、本季度、本月、上月的开端日期、中止日期

一、首先来一个本身公司项目的源码:

项目需求描述: 从20150712日开始, ,html

需求①:根据当前时间返回每一周 、周一~周日的日期(需返回2种格式 格式1:7月13日,格式2:2015-07-13)jquery

需求②:返回当前时间本周的,周一早上9:00:00和周日的23:59:59spring

需求③:计算出当前时间离20150712日有多少周json

需求④:在需求3基础上,若是大于一周,须要返回历史周的周一早上9:00:00和周日的23:59:59  (在此用二位数组)数组

function GetWeekDate(time){
    var now = new Date(time); //当前日期 
    this.nowDayOfWeek = now.getDay(); //今天本周的第几天
    this.nowYear = now.getYear(); //当前年 
    this.nowMonth = now.getMonth(); //月 
    this.nowDay = now.getDate(); //日 
    this.beginHour="09:00:00";
    this.endHour="23:59:59";

    this.nowYear += (this.nowYear < 2000) ? 1900 : 0; //
    this.nowDayOfWeek = this.nowDayOfWeek==0?7:this.nowDayOfWeek; // 若是是周日,就变成周七
}

GetWeekDate.prototype.date2str=function(x,y){//date2str(new Date(curTime),"yyyy-MM-dd")
    var z ={y:x.getFullYear(),M:x.getMonth()+1,d:x.getDate(),h:x.getHours(),m:x.getMinutes(),s:x.getSeconds()};  
    return y.replace(/(y+|M+|d+|h+|m+|s+)/g,function(v) {return ((v.length>1?"0":"")+eval('z.'+v.slice(-1))).slice(-(v.length>2?v.length:2))});  
}

GetWeekDate.prototype.formatDate=function(date){//格局化日期:yyyy-MM-dd 
    var myyear = date.getFullYear(); 
    var mymonth = date.getMonth()+1; 
    var myweekday = date.getDate();
    //alert("formatDate"+myyear+":"+mymonth+":"+myweekday)
    if(mymonth < 10){ 
        mymonth = "0" + mymonth; 
    } 
    if(myweekday < 10){ 
        myweekday = "0" + myweekday; 
    } 
    return (myyear+"-"+mymonth + "-" + myweekday); 
}

GetWeekDate.prototype.getWeekStartDate=function(){ //得到本周的开端日期
    var weekStartDate = new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek+1);
    return this.formatDate(weekStartDate); 
}
 
GetWeekDate.prototype.getWeekEndDate=function(){//得到本周的中止日期
    var weekEndDate = new Date(this.nowYear, this.nowMonth, this.nowDay + (6 - this.nowDayOfWeek+1));
    return this.formatDate(weekEndDate); 
}

GetWeekDate.prototype.getAWeedkYMD=function(){//得到本周周一~周日的年月日
    var ymdArr=[];
    for (var i = 0; i < 7; i++) {
    	ymdArr[i]=[];
    	//ymdArr[i][0]=this.formatDate(new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek+i+1));
    	ymdArr[i][0]=this.date2str(new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek+i+1),'yyyy-MM-dd');
    	ymdArr[i][1]=this.date2str(new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek+i+1), 'MM月dd日');
    };
    
    return ymdArr;
}

GetWeekDate.prototype.getQishu=function(time){//得到本周是指定日期(7.12日)的第几周
    var oNDate=new Date(time); //系统当前时间
    var oEDate=new Date('2015/7/12 23:59:59');
    var diff= oNDate.getTime()-oEDate.getTime();
    //console.log(diff)
    //console.log("相差天:"+diff/(24*60*60*1000))
    //console.log("相差期数:"+parseInt(diff/(24*60*60*1000))/7)
    //console.log("取整期数:"+Math.floor(parseInt(diff/(24*60*60*1000))/7))
    return (diff/(24*60*60*1000))/7;
}

GetWeekDate.prototype.getWeeksDates=function(time){//获取历史周排行的周一到周日时间段
    var qishu = (this.qishu || this.qishu==0)?this.qishu:this.getQishu(time);
    //var qishu=this.getQishu(time);
    
    var WeeksTimes=[];//存放时间的二维数组
        minYear=2015,
        minMonth=7,
        minDay=12;
    for (var i = 0; i<qishu; i++) {
        var sday;
        var eday;
        WeeksTimes[i]=[];
        if(i==0){//若是离2015-07-12只相差1期
            sday=minDay+1; //开始时间+1天
        }else{//若是离2015-07-12相差期数>1
            sday=minDay+1+7*i;
        }
        eday=minDay+7*(i+1);//结束时间+7天
        WeeksTimes[i][0]=this.formatDate(new Date(minYear,minMonth-1,sday))+" "+this.beginHour;
        WeeksTimes[i][1]=this.formatDate(new Date(minYear,minMonth-1,eday))+" "+this.endHour;
    };
    //若是是一周的周日就不要减去一期
    return WeeksTimes;
    
}

GetWeekDate.prototype.todayData=function(json){//处理tender_list_week.jsp页面
    var oQishu=$('.qishu');
    	iQishu=this.qishu;//期数+1是由于,相差0期就是第1期
    	oThisWeekListBtn=$('.total_list_btn'),//查看本周排行榜按钮
    	sTime=this.getWeekStartDate()+" "+this.beginHour,
        eTime=this.getWeekEndDate()+" "+this.endHour;
    //一、修改期数
    	//console.log("相差周:"+iQishu);
	if(/^[1-9]\d*$/.test(iQishu) || iQishu==0){//整数 +3
    	oQishu.html(iQishu*1+3); 
    }else{
    	oQishu.html(Math.floor(iQishu)*1+4);
    }
    oThisWeekListBtn.attr("onclick","showWeekList('"+sTime+"','"+eTime+"')");//二、修改查看本周排行榜的起止时间
}

GetWeekDate.prototype.historyData=function(time){//处理tender_list_week.jsp页面
    var oQishu=$('.qishu'),
        oDateList=$('#dateList'), //顶部导航时间
        oHistoryList=$('#history_cont'),//历史周
    	aThisWeekYMD=this.getAWeedkYMD(),//本周周一~周日的年月日
    	aThisWeekDates=this.getWeeksDates(time);//获取历史周排行的周一到周日时间段
    	iQishu=this.qishu;
    	//console.log("相差周:"+iQishu);
    
    //一、修改期数,+4是加上前3期,在+1是若是间隔3期,当前就是第4期
    	//若是时间23:59:59是正整数(-1),若是是0整数(3),若是是非整数(向下取整),或负数(向上取整)可是负数不考虑
    //oQishu.html(iQishu*1+3);
    if(/^[1-9]\d*$/.test(iQishu) || iQishu==0){//整数 +3
    	oQishu.html(iQishu*1+3); 
    }else{
    	oQishu.html(Math.floor(iQishu)*1+4);
    }
    
    
    //二、给顶部时间追加时间
    for (var i = 0; i < aThisWeekYMD.length; i++) {
    	var str='<li data-time="'+aThisWeekYMD[i][0]+'">'+aThisWeekYMD[i][1]+'</li>';
        $(str).appendTo(oDateList);
    };
    
    //三、给历史周排行榜添加周期数
    if(this.qishu>1){//若是相差的期数大于1期
    	//console.dir(aThisWeekDates)
    	for (var j = 0; j < aThisWeekDates.length-1; j++) {
    		var iQiNum=j+4;
        	var str='<li οnclick="showWeekList(\''+aThisWeekDates[j][0]+'\',\''+aThisWeekDates[j][1]+'\')">第'+iQiNum+'期</li>';
            $(str).prependTo(oHistoryList);
        };
    }
    
}

GetWeekDate.prototype.init=function(time){
    var iQishu=this.getQishu(time),//期数+1是由于,相差0期就是第1期
        json={};
    json.qishu=iQishu;
    this.qishu=iQishu;

    if($('#pageType').val()=="today"){//若是是tender_list_week.jsp页面
    	if(new Date(time).getTime() < new Date('2015/07/12 23:59:59').getTime()){//特殊处理时间小于20150712
    		$('.qishu').html('3');
    		$('.total_list_btn').attr("onclick","showWeekList('2015-07-03 09:00:00','2015-07-12 23:59:59')");
    		return false;
    	}
        this.todayData(json);
    }else if($('#pageType').val()=="history"){
    	if(new Date(time).getTime() < new Date('2015/07/12 23:59:59').getTime()){//特殊处理时间小于20150712
    		$('.qishu').html('3');
    		$('#dateList').addClass('dateList2').html('<li data-time="2015-07-03">07月03日</li><li data-time="2015-07-04">07月04日</li><li data-time="2015-07-05">07月05日</li><li data-time="2015-07-06">07月06日</li><li data-time="2015-07-07">07月07日</li><li data-time="2015-07-08">07月08日</li><li data-time="2015-07-09">07月09日</li><li data-time="2015-07-10">07月10日</li><li data-time="2015-07-11">07月11日</li><li data-time="2015-07-12">07月12日</li>');
    		$('#history_cont').html('<li οnclick="showWeekList(\'2015-06-26 09:00:00\',\'2015-07-02 23:59:59\')">第二期</li><li οnclick="showWeekList(\'2015-06-19 12:00:00\',\'2015-06-25 23:59:59\')">第一期</li>');
    		return false;
    	}
        this.historyData(time);
    }
    //console.dir(this.getWeeksDates(time));
};

二、JS获取本周、本季度、本月、上月的开端日期、中止日期app

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
    /**
* 日期范围工具类
*/
var dateRangeUtil = (function () {
    /***
    * 得到当前时间
    */
    this.getCurrentDate = function () {
        return new Date('2015','07','-1');
    };

    /***
    * 得到本周起止时间
    */
    this.getCurrentWeek = function () {
        //起止日期数组  
        var startStop = new Array();
        //获取当前时间  
        var currentDate = this.getCurrentDate();
        //返回date是一周中的某一天  
        var week = currentDate.getDay();
        //返回date是一个月中的某一天  
        var month = currentDate.getDate();

        //一天的毫秒数  
        var millisecond = 1000 * 60 * 60 * 24;
        //减去的天数  
        var minusDay = week != 0 ? week - 1 : 6;
        //alert(minusDay);  
        //本周 周一  
        var monday = new Date(currentDate.getTime() - (minusDay * millisecond));
        //本周 周日  
        var sunday = new Date(monday.getTime() + (6 * millisecond));
        //添加本周时间  
        startStop.push(monday); //本周起始时间  
        //添加本周最后一天时间  
        startStop.push(sunday); //本周终止时间  
        //返回  
        return startStop;
    };

    /***
    * 得到本月的起止时间
    */
    this.getCurrentMonth = function () {
        //起止日期数组  
        var startStop = new Array();
        //获取当前时间  
        var currentDate = this.getCurrentDate();
        //得到当前月份0-11  
        var currentMonth = currentDate.getMonth();
        //得到当前年份4位年  
        var currentYear = currentDate.getFullYear();
        //求出本月第一天  
        var firstDay = new Date(currentYear, currentMonth, 1);


        //当为12月的时候年份须要加1  
        //月份须要更新为0 也就是下一年的第一个月  
        if (currentMonth == 11) {
            currentYear++;
            currentMonth = 0; //就为  
        } else {
            //不然只是月份增长,以便求的下一月的第一天  
            currentMonth++;
        }


        //一天的毫秒数  
        var millisecond = 1000 * 60 * 60 * 24;
        //下月的第一天  
        var nextMonthDayOne = new Date(currentYear, currentMonth, 1);
        //求出上月的最后一天  
        var lastDay = new Date(nextMonthDayOne.getTime() - millisecond);

        //添加至数组中返回  
        startStop.push(firstDay);
        startStop.push(lastDay);
        //返回  
        return startStop;
    };

    /**
    * 获得本季度开始的月份
    * @param month 须要计算的月份
    ***/
    this.getQuarterSeasonStartMonth = function (month) {
        var quarterMonthStart = 0;
        var spring = 0; //春  
        var summer = 3; //夏  
        var fall = 6;   //秋  
        var winter = 9; //冬  
        //月份从0-11  
        if (month < 3) {
            return spring;
        }

        if (month < 6) {
            return summer;
        }

        if (month < 9) {
            return fall;
        }

        return winter;
    };

    /**
    * 得到该月的天数
    * @param year年份
    * @param month月份
    * */
    this.getMonthDays = function (year, month) {
        //本月第一天 1-31  
        var relativeDate = new Date(year, month, 1);
        //得到当前月份0-11  
        var relativeMonth = relativeDate.getMonth();
        //得到当前年份4位年  
        var relativeYear = relativeDate.getFullYear();

        //当为12月的时候年份须要加1  
        //月份须要更新为0 也就是下一年的第一个月  
        if (relativeMonth == 11) {
            relativeYear++;
            relativeMonth = 0;
        } else {
            //不然只是月份增长,以便求的下一月的第一天  
            relativeMonth++;
        }
        //一天的毫秒数  
        var millisecond = 1000 * 60 * 60 * 24;
        //下月的第一天  
        var nextMonthDayOne = new Date(relativeYear, relativeMonth, 1);
        //返回获得上月的最后一天,也就是本月总天数  
        return new Date(nextMonthDayOne.getTime() - millisecond).getDate();
    };

    /**
    * 得到本季度的起止日期
    */
    this.getCurrentSeason = function () {
        //起止日期数组  
        var startStop = new Array();
        //获取当前时间  
        var currentDate = this.getCurrentDate();
        //得到当前月份0-11  
        var currentMonth = currentDate.getMonth();
        //得到当前年份4位年  
        var currentYear = currentDate.getFullYear();
        //得到本季度开始月份  
        var quarterSeasonStartMonth = this.getQuarterSeasonStartMonth(currentMonth);
        //得到本季度结束月份  
        var quarterSeasonEndMonth = quarterSeasonStartMonth + 2;

        //得到本季度开始的日期  
        var quarterSeasonStartDate = new Date(currentYear, quarterSeasonStartMonth, 1);
        //得到本季度结束的日期  
        var quarterSeasonEndDate = new Date(currentYear, quarterSeasonEndMonth, this.getMonthDays(currentYear, quarterSeasonEndMonth));
        //加入数组返回  
        startStop.push(quarterSeasonStartDate);
        startStop.push(quarterSeasonEndDate);
        //返回  
        return startStop;
    };

    /***
    * 获得本年的起止日期
    * 
    */
    this.getCurrentYear = function () {
        //起止日期数组  
        var startStop = new Array();
        //获取当前时间  
        var currentDate = this.getCurrentDate();
        //得到当前年份4位年  
        var currentYear = currentDate.getFullYear();

        //本年第一天  
        var currentYearFirstDate = new Date(currentYear, 0, 1);
        //本年最后一天  
        var currentYearLastDate = new Date(currentYear, 11, 31);
        //添加至数组  
        startStop.push(currentYearFirstDate);
        startStop.push(currentYearLastDate);
        //返回  
        return startStop;
    };

    /**
    * 返回上一个月的第一天Date类型
    * @param year 年
    * @param month 月
    **/
    this.getPriorMonthFirstDay = function (year, month) {
        //年份为0表明,是本年的第一月,因此不能减  
        if (month == 0) {
            month = 11; //月份为上年的最后月份  
            year--; //年份减1  
            return new Date(year, month, 1);
        }
        //不然,只减去月份  
        month--;
        return new Date(year, month, 1); ;
    };

    /**
    * 得到上一月的起止日期
    * ***/
    this.getPreviousMonth = function () {
        //起止日期数组  
        var startStop = new Array();
        //获取当前时间  
        var currentDate = this.getCurrentDate();
        //得到当前月份0-11  
        var currentMonth = currentDate.getMonth();
        //得到当前年份4位年  
        var currentYear = currentDate.getFullYear();
        //得到上一个月的第一天  
        var priorMonthFirstDay = this.getPriorMonthFirstDay(currentYear, currentMonth);
        //得到上一月的最后一天  
        var priorMonthLastDay = new Date(priorMonthFirstDay.getFullYear(), priorMonthFirstDay.getMonth(), this.getMonthDays(priorMonthFirstDay.getFullYear(), priorMonthFirstDay.getMonth()));
        //添加至数组  
        startStop.push(priorMonthFirstDay);
        startStop.push(priorMonthLastDay);
        //返回  
        return startStop;
    };


    /**
    * 得到上一周的起止日期
    * **/
    this.getPreviousWeek = function () {
        //起止日期数组  
        var startStop = new Array();
        //获取当前时间  
        var currentDate = this.getCurrentDate();
        //返回date是一周中的某一天  
        var week = currentDate.getDay();
        //返回date是一个月中的某一天  
        var month = currentDate.getDate();
        //一天的毫秒数  
        var millisecond = 1000 * 60 * 60 * 24;
        //减去的天数  
        var minusDay = week != 0 ? week - 1 : 6;
        //得到当前周的第一天  
        var currentWeekDayOne = new Date(currentDate.getTime() - (millisecond * minusDay));
        //上周最后一天即本周开始的前一天  
        var priorWeekLastDay = new Date(currentWeekDayOne.getTime() - millisecond);
        //上周的第一天  
        var priorWeekFirstDay = new Date(priorWeekLastDay.getTime() - (millisecond * 6));

        //添加至数组  
        startStop.push(priorWeekFirstDay);
        startStop.push(priorWeekLastDay);

        return startStop;
    };

    /**
    * 获得上季度的起始日期
    * year 这个年应该是运算后获得的当前本季度的年份
    * month 这个应该是运算后获得的当前季度的开始月份
    * */
    this.getPriorSeasonFirstDay = function (year, month) {
        var quarterMonthStart = 0;
        var spring = 0; //春  
        var summer = 3; //夏  
        var fall = 6;   //秋  
        var winter = 9; //冬  
        //月份从0-11  
        switch (month) {//季度的其实月份  
            case spring:
                //若是是第一季度则应该到去年的冬季  
                year--;
                month = winter;
                break;
            case summer:
                month = spring;
                break;
            case fall:
                month = summer;
                break;
            case winter:
                month = fall;
                break;

        };

        return new Date(year, month, 1);
    };

    /**
    * 获得上季度的起止日期
    * **/
    this.getPreviousSeason = function () {
        //起止日期数组  
        var startStop = new Array();
        //获取当前时间  
        var currentDate = this.getCurrentDate();
        //得到当前月份0-11  
        var currentMonth = currentDate.getMonth();
        //得到当前年份4位年  
        var currentYear = currentDate.getFullYear();
        //上季度的第一天  
        var priorSeasonFirstDay = this.getPriorSeasonFirstDay(currentYear, currentMonth);
        //上季度的最后一天  
        var priorSeasonLastDay = new Date(priorSeasonFirstDay.getFullYear(), priorSeasonFirstDay.getMonth() + 2, this.getMonthDays(priorSeasonFirstDay.getFullYear(), priorSeasonFirstDay.getMonth() + 2));
        //添加至数组  
        startStop.push(priorSeasonFirstDay);
        startStop.push(priorSeasonLastDay);
        return startStop;
    };

    /**
    * 获得去年的起止日期
    * **/
    this.getPreviousYear = function () {
        //起止日期数组  
        var startStop = new Array();
        //获取当前时间  
        var currentDate = this.getCurrentDate();
        //得到当前年份4位年  
        var currentYear = currentDate.getFullYear();
        currentYear--;
        var priorYearFirstDay = new Date(currentYear, 0, 1);
        var priorYearLastDay = new Date(currentYear, 11, 1);
        //添加至数组  
        startStop.push(priorYearFirstDay);
        startStop.push(priorYearLastDay);
        return startStop;
    };
    return this;
    
})();

$(function(){

    $("#startDate").html( dateRangeUtil.getCurrentWeek() );
})

</script>
</head>
<body>
    <div id="startDate"></div>
</body>
</html>

三、JS获取本周、本季度、本月、上月的开端日期、中止日期jsp

Js代码   
/** 
* 获取本周、本季度、本月、上月的开端日期、中止日期 
*/ 
var now = new Date(); //当前日期 
var nowDayOfWeek = now.getDay(); //今天本周的第几天 
var nowDay = now.getDate(); //当前日 
var nowMonth = now.getMonth(); //当前月 
var nowYear = now.getYear(); //当前年 
nowYear += (nowYear < 2000) ? 1900 : 0; //

var lastMonthDate = new Date(); //上月日期 
lastMonthDate.setDate(1); 
lastMonthDate.setMonth(lastMonthDate.getMonth()-1); 
var lastYear = lastMonthDate.getYear(); 
var lastMonth = lastMonthDate.getMonth();

//格局化日期:yyyy-MM-dd 
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 getMonthDays(myMonth){ 
var monthStartDate = new Date(nowYear, myMonth, 1); 
var monthEndDate = new Date(nowYear, myMonth + 1, 1); 
var days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24); 
return days; 
}

//得到本季度的开端月份 
function getQuarterStartMonth(){ 
var quarterStartMonth = 0; 
if(nowMonth<3){ 
quarterStartMonth = 0; 
} 
if(2<nowMonth && nowMonth<6){ 
quarterStartMonth = 3; 
} 
if(5<nowMonth && nowMonth<9){ 
quarterStartMonth = 6; 
} 
if(nowMonth>8){ 
quarterStartMonth = 9; 
} 
return quarterStartMonth; 
}

//得到本周的开端日期 
function getWeekStartDate() { 
var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek); 
return formatDate(weekStartDate); 
}

//得到本周的中止日期 
function getWeekEndDate() { 
var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek)); 
return formatDate(weekEndDate); 
}

//得到本月的开端日期 
function getMonthStartDate(){ 
var monthStartDate = new Date(nowYear, nowMonth, 1); 
return formatDate(monthStartDate); 
}

//得到本月的中止日期 
function getMonthEndDate(){ 
var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth)); 
return formatDate(monthEndDate); 
}

//得到上月开端时候 
function getLastMonthStartDate(){ 
var lastMonthStartDate = new Date(nowYear, lastMonth, 1); 
return formatDate(lastMonthStartDate); 
}

//得到上月中止时候 
function getLastMonthEndDate(){ 
var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth)); 
return formatDate(lastMonthEndDate); 
}

//得到本季度的开端日期 
function getQuarterStartDate(){

var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1); 
return formatDate(quarterStartDate); 
}

//或的本季度的中止日期 
function getQuarterEndDate(){ 
var quarterEndMonth = getQuarterStartMonth() + 2; 
var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth)); 
return formatDate(quarterStartDate); 
}