您的位置:首页 > Web前端 > JavaScript

在js中如何获取上一个季度的开始、结束日期

2018-03-23 14:03 686 查看
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;    
    }    
    
    var dayMSec = 24 * 3600 * 1000;    
    var today = new Date();    
        
    //得到今天距离本周一的天数    
    function getDayBetweenMonday(){    
        //得到今天的星期数(0-6),星期日为0    
        var weekday = today.getDay();    
        //周日    
        if(weekday == 0){    
            return 6;    
        }else{    
            return weekday - 1;    
        }    
    }    
        
    function getLastDay(){    
            
        var yestodayMSec=today.getTime() -dayMSec;    
            
        var yestoday = new Date(yestodayMSec);    
            
        return yestoday.format('yyyy-MM-dd')+","+yestoday.format('yyyy-MM-dd');    
    }    
        
    function getLastWeek(){    
        //得到距离本周一的天数    
        var weekdayBetween = getDayBetweenMonday();    
            
        //得到本周星期一的毫秒值    
        var nowMondayMSec = today.getTime() - weekdayBetween * dayMSec;    
        //得到上周一的毫秒值    
        var lastMondayMSec = nowMondayMSec - 7 * dayMSec;    
        //得到上周日的毫秒值    
        var lastSundayMSec = nowMondayMSec - 1 * dayMSec;    
            
        var lastMonday = new Date(lastMondayMSec);    
            
        var lastSunday = new Date(lastSundayMSec);    
            
            
        return lastMonday.format('yyyy-MM-dd')+","+lastSunday.format('yyyy-MM-dd');    
    }    
        
    function getLastMonth(){    
        //得到上一个月的第一天    
        var lastMonthFirstDay = new Date(today.getFullYear() , today.getMonth()-1 , 1);    
        //得到本月第一天    
        var nowMonthFirstDay = new Date(today.getFullYear() , today.getMonth(), 1);    
        //得到上一个月的最后一天的毫秒值    
        var lastMonthLastDayMSec = nowMonthFirstDay.getTime() - 1 * dayMSec;    
        var lastMonthLastDay = new Date(lastMonthLastDayMSec);    
            
        return lastMonthFirstDay.format('yyyy-MM-dd')+","+lastMonthLastDay.format('yyyy-MM-dd');    
    }    
        
    function getLastQuarter(){    
        //得到上一个季度的第一天    
        var lastQuarterFirstDay = new Date(today.getFullYear() , today.getMonth() - 3 , 1);    
        //得到本月第一天    
        var nowMonthFirstDay = new Date(today.getFullYear() , today.getMonth(), 1);    
        //得到上一个季度的最后一天的毫秒值    
        var lastQuarterLastDayMSec = nowMonthFirstDay.getTime() - 1 * dayMSec;    
        var lastQuarterLastDay = new Date(lastQuarterLastDayMSec);    
            
        return lastQuarterFirstDay.format('yyyy-MM-dd') +","+lastQuarterLastDay.format('yyyy-MM-dd');    
    }    

 常说的季度规则。
调用getLastQuarter();就行了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  javaScript
相关文章推荐