var realDateUtil = {
    getRealDate(type) {
        var _this = {};
        var date = new Date();
        var nd = date.getDay();
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        var d = date.getDate();
        var _date = y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d)
        var q = 1;
        switch (true) {
            case m < 4:
                q = 1;
                break;
            case m > 3 && m < 7:
                q = 2;
                break;
            case m > 6 && m < 10:
                q = 3;
                break;
            case m > 9 && m < 13:
                q = 4;
                break;
        }
        var lastMonthDate = new Date(); //上月日期
        lastMonthDate.setDate(1);
        lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
        var lastYear = lastMonthDate.getYear();
        var lastMonth = lastMonthDate.getMonth();

        if (type == 'now') { //本月
            _this.startDate = this.beforeDate(new Date(y, m - 1, 1), 0);
            _this.endDate = this.beforeDate(new Date(y, m - 1, this.getMonthDays(m - 1, y), 0));
        } else if (type == 'prev') {
            _this.startDate = this.beforeDate(new Date(y, lastMonth, 1), 0);
            _this.endDate = this.beforeDate(new Date(y, lastMonth, this.getMonthDays(lastMonth, y), 0));
        } else if (type == 'j') {
            // 当季度
            _this.startDate = this.beforeDate(new Date(y, this.getQuarterStartMonth(m), 1), 0);
            var quarterEndMonth = this.getQuarterStartMonth(m) + 2;
            _this.endDate = this.beforeDate(new Date(y, quarterEndMonth, this.getMonthDays(quarterEndMonth, y), 0));
        } else if (type == 'pj') {
            // 上季度
            _this.startDate = this.beforeDate(new Date(y, this.getQuarterStartMonth(m - 3), 1), 0);
            var quarterEndMonth = this.getQuarterStartMonth(m - 3) + 2;
            _this.endDate = this.beforeDate(new Date(y, quarterEndMonth, this.getMonthDays(quarterEndMonth, y), 0));
        } else if (type == 'y') {
            // 今年
            _this.startDate = y + '-' + '01-01';
            _this.endDate = y + '-' + '12-31';
        } else if (type == 'py') {
            // 去年
            var _y = y - 1;
            _this.startDate = _y + '-' + '01-01';
            _this.endDate = _y + '-' + '12-31';
        } else if (type == '1') {
            _this.startDate = this.beforeDate(_date, parseInt(type));
            _this.endDate = this.beforeDate(_date, parseInt(type));
        } else if (type == 'bz') {
            _this.startDate = this.beforeDate(new Date(y, m - 1, d - nd), 0);
            _this.endDate = this.beforeDate(new Date(y, m - 1, d + (6 - nd)), 0);
        } else if (type == 'sz') {
            _this.startDate = this.beforeDate(new Date(y, m - 1, d + (nd - 13)), 0);
            _this.endDate = this.beforeDate(new Date(y, m - 1, d + (nd - 7)), 0);
        } else {
            _this.startDate = this.beforeDate(_date, parseInt(type));
            _this.endDate = _date;
        }
        return _this;
    },
    // 前n天日期
    beforeDate(date, days) {
        if (days == undefined || days == '') {
            days = 0;
        }
        if (days == undefined || days == '') {
            days = 0;
        }
        date = date ? new Date(date) : new Date();
        date.setDate(date.getDate() - days);
        var month = date.getMonth() + 1;
        var day = date.getDate();
        return date.getFullYear() + '-' + this.getFormatDate(month) + '-' + this.getFormatDate(day);
    },
    //获得某月的天数
    getMonthDays(myMonth, y) {
        var monthStartDate = new Date(y, myMonth, 1);
        var monthEndDate = new Date(y, myMonth + 1, 1);
        var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
        return days;
    },
    //获得本季度的开端月份
    getQuarterStartMonth(m) {
        var quarterStartMonth = 0;
        if (m < 3) {
            quarterStartMonth = 0;
        }
        if (m > 2 && m < 6) {
            quarterStartMonth = 3;
        }
        if (m > 5 && m < 9) {
            quarterStartMonth = 6;
        }
        if (m > 8) {
            quarterStartMonth = 9;
        }
        return quarterStartMonth;
    },
    getFormatDate(arg) {
        if (arg == undefined || arg == '') {
            return '';
        }
        var re = arg + '';
        if (re.length < 2) {
            re = '0' + re;
        }
        return re;
    }
}

export default realDateUtil