js上周(月、季)、上上周(月、季)、去年同期上周(月、季)

这是一个JavaScript函数集合,用于获取当前时间、今年、昨天、本周、本月、本季度、上个月、上上周、上个季度和去年相同时间周期的起止日期。通过这些函数,可以方便地进行时间周期的数据对比和分析。
摘要由CSDN通过智能技术生成
    /***
     * 获得当前时间
     */ 
function getCurrentDate(){ 
        return new Date(); 
    }; 
    
//今年以来的起止时间段
function getThisYear(){
	//起止日期数组  
			var startStop=new Array(); 
			//获取当前时间  
      var currentDate=this.getCurrentDate(); 
      var stop = currentDate.getYear()+"-"+(currentDate.getMonth()+1)+"-"+currentDate.getDate();
      var start = currentDate.getYear()+"-"+"01-01";
      
      //添加年初时间  
      startStop.push(start);//本周起始时间  
      //添加当前时间
      startStop.push(stop);//本周终止时间  
      //返回  
      return startStop; 
}
 //获得昨日起止时间   
function getYesterdayDate(){
		//起止日期数组  
			var startStop=new Array(); 
			//获取当前时间  
      var currentDate=this.getCurrentDate(); 
      //一天的毫秒数  
      var millisecond=1000*60*60*24; 
      //昨日时间
      yesterday = new Date(currentDate.getTime()-millisecond);
      currentDate = currentDate.getYear()+"-"+(currentDate.getMonth()+1)+"-"+currentDate.getDate();
      yesterday = yesterday.getYear()+"-"+(yesterday.getMonth()+1)+"-"+yesterday.getDate();
      //添加本周时间  
      startStop.push(yesterday);//本周起始时间  
      //添加本周最后一天时间  
      startStop.push(currentDate);//本周终止时间  
      //返回  
      return startStop; 
}
    /***
     * 获得本周起止时间
     */ 
function getCurrentWeek(){ 
        //起止日期数组  
        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)); 
        monday = monday.getYear()+"-"+(monday.getMonth()+1)+"-"+monday.getDate();
        sunday = sunday.getYear()+"-"+(sunday.getMonth()+1)+"-"+sunday.getDate();
        //添加本周时间  
        startStop.push(monday);//本周起始时间  
        //添加本周最后一天时间  
        startStop.push(sunday);//本周终止时间  
        //返回  
        return startStop; 
    }; 
    /***
     * 获得本月的起止时间
     */ 
function getCurrentMonth(){ 
        //起止日期数组  
        var startStop=new Array(); 
        //获取当前时间  
        var currentDate=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); 
        firstDay = firstDay.getYear()+"-"+(firstDay.getMonth()+1)+"-"+firstDay.getDate();
        lastDay = lastDay.getYear()+"-"+(lastDay.getMonth()+1)+"-"+lastDay.getDat
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值