/*
 *公共方法 
 *时间格式
 *
 *
 */

let DateUtil = {
    getDistanceSpecifiedTime(dateTime) {
        if (!dateTime) {
            return;
        }
        // 指定日期和时间
        var EndTime = new Date(dateTime.replace(/-/g, '/'));
        // 当前系统时间
        var NowTime = new Date();
        var t = EndTime.getTime() - NowTime.getTime();
        var d = Math.floor(t / 1000 / 60 / 60 / 24);
        var h = Math.floor(t / 1000 / 60 / 60 % 24);
        var m = Math.floor(t / 1000 / 60 % 60);
        var s = Math.floor(t / 1000 % 60);
        // var html = d + " 天" + h + " 时" + m + " 分" + s + " 秒";
        return { day: d, h: h, m: m, s: s }
    },
    //业务 需要 比较时间大小 相距间隔
    compareTimeDistance: (checkTime, baseTime) => {
        let rstInfo = {
            canBeginFlag: false,
            minuteTimes: 0,
            dayDistance: ''

        }
        let newCheckTime = '';
        let newBaseTime = '';
        if (baseTime) {
            newBaseTime = Date.parse(baseTime.replace(/-/g, '/'));
        } else {
            newBaseTime = Date.parse(new Date());
        }
        // 判断日期是不是今天、昨天、明天

        let d = new Date(checkTime.replace(/-/g, '/')).setHours(0, 0, 0, 0);
        let today = new Date().setHours(0, 0, 0, 0);

        let obj = {
            '-86400000': 'yestoday',
            0: 'today',
            86400000: 'tomorrow',
        };
        let jian = d - today;
        if (jian > 86400000) {
            rstInfo.dayDistance = 'afterTomorrow';
        } else if (jian < -86400000) {
            rstInfo.dayDistance = 'beforeYestoday';
        } else if (jian == '-86400000') {
            rstInfo.dayDistance = 'yestoday';
        } else if (jian == '0') {
            rstInfo.dayDistance = 'today';
        } else if (jian == '86400000') {
            rstInfo.dayDistance = 'tomorrow';
        }
        if (checkTime) {
            newCheckTime = Date.parse(checkTime.replace(/-/g, '/'));
            // console.log(newCheckTime,newBaseTime,newCheckTime < newBaseTime)
            if (newCheckTime < newBaseTime) {
                //比标准值小
                rstInfo.canBeginFlag = true;
                //小多少
                rstInfo.minuteTimes = newBaseTime - newCheckTime;
            } else {
                //小多少
                rstInfo.canBeginFlag = false;
                rstInfo.minuteTimes = newCheckTime - newBaseTime;
            }
        }
        return rstInfo;
    },
    //获取时间字符串  date-->   201808081208
    getTimestamp: () => {
        let date = new Date();
        let month = (date.getMonth() + 1).toString();
        let strDate = (date.getDate()).toString();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        let currentdate = date.getFullYear() + month + strDate + date.getHours() + date.getMinutes() + date.getSeconds();

        return currentdate
    },
    //获取当日时间字符串  date-->   2018-08-08
    getTimesStr: () => {
        let date = new Date();
        let month = (date.getMonth() + 1).toString();
        let strDate = (date.getDate()).toString();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        let currentdate = date.getFullYear() + '-' + month + '-' + strDate;
        // this.$store.state.date = currentdate; //转换格式为yyyy-mm-dd
        let dateDot = date.toLocaleDateString().replace(/\//g, ".");
        let monthNum = (date.getMonth() + 1).toString();//没有0的当月时间字符串
        let monthStr = date.getFullYear() + "-" + month
        return {currentdate:currentdate,dateDot:dateDot,monthNum:monthNum,monthStr:monthStr}
    },
    //获取月份字符串  date-->   2018-08-08
    getMonthStr:(month)=> {
        let date = new Date();
        let strDate = (date.getDate()).toString();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        let monthStr = date.getFullYear() + "-" + month
        return monthStr
    },
    //获取近n个月的时间 返回值是数组 当前时间和上n个月的时间
    getLastMonth: (n) => {
        //获取系统当前时间
        var nowdate = new Date();
        var y = nowdate.getFullYear();
        var m = nowdate.getMonth() + 1;
        var d = nowdate.getDate();
        var formatnowdate = y + '-' + m + '-' + d;


        //获取系统前一个月的时间
        nowdate.setMonth(nowdate.getMonth() - n);
        var y = nowdate.getFullYear();
        var m = nowdate.getMonth() + 1;
        var d = nowdate.getDate();
        var formatlastmonth = y + '-' + m + '-' + d;

        return [formatlastmonth, formatnowdate]
    },

    //date格式转 时间字符串  date--> 2018-05-05 12:00:00
    dateToString: () => {
        var date = new Date();
        var year = date.getFullYear();
        var month = (date.getMonth() + 1).toString();
        var day = (date.getDate()).toString();
        var hour = (date.getHours()).toString();
        var minute = (date.getMinutes()).toString();
        var second = (date.getSeconds()).toString();
        if (month.length == 1) {
            month = "0" + month;
        }
        if (day.length == 1) {
            day = "0" + day;
        }
        if (hour.length == 1) {
            hour = "0" + hour;
        }
        if (minute.length == 1) {
            minute = "0" + minute;
        }
        if (second.length == 1) {
            second = "0" + second;
        }
        var dateTime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
        return dateTime;
    },
    //num:0 YYYY-MM-DD  num:1  YYYY-MM-DD hh:mm:ss // timestamp:时间戳 
    numToDate: (timestamp,num) => {
        // let sStr = secondStr.substring(secondStr.length-4 , secondStr.length)
        // return new Date(parseInt(sStr) * 1000).toLocaleString().replace(/:\d{1,2}$/,' '); 
        timestamp = timestamp + '';
        timestamp = timestamp.length==10 ? timestamp*1000 : timestamp;
        var date = new Date(timestamp);
        var y = date.getFullYear();  
        var m = date.getMonth() + 1;  
        m = m < 10 ? ('0' + m) : m;  
        var d = date.getDate();  
        d = d < 10 ? ('0' + d) : d;  
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        var second = date.getSeconds();
        minute = minute < 10 ? ('0' + minute) : minute;  
        second = second < 10 ? ('0' + second) : second; 
        if(num==0){
            return y + '-' + m + '-' + d;  
        }else{
            return y + '-' + m + '-' + d +' '+ h +':'+ minute +':' + second;  
        }
    },
    /**
     * @param {Number} timeStamp 传入的时间戳
     * @param {Number} startType 要返回的时间字符串的格式类型，传入'year'则返回年开头的完整时间
     * 时间戳转时间格式
     */
    getDate: (dateStr) => {
        var date = new Date()
        if (dateStr) {
        date = new Date(dateStr)
        }
        var y = date.getFullYear()
        var m = date.getMonth() + 1
        var d = date.getDate()

        var h = date.getHours()
        var mm = date.getMinutes()
        var s = date.getSeconds()
        var ds =
        y +
        "-" +
        (m < 10 ? "0" + m : m + "") +
        "-" +
        (d < 10 ? "0" + d : d + "") +
        " " +
        (h < 10 ? "0" + h : h + "") +
        ":" +
        (mm < 10 ? "0" + mm : mm + "") +
        ":" +
        (s < 10 ? "0" + s : s + "")
        return ds
  },

}

export {
    DateUtil
};