時間轉換(日期時間與時間戳)


1.初始化查詢,查詢當天日期(年月日,2018-02-15),時間戳處理實際獲取的時間是時分秒的時間戳,而實際只需要獲取年月日的零點時間戳

//當前13位數的時間戳,去掉時分秒,即為當天零點時間戳
        let nowT = new Date();
        let endT = new Date(nowT).getTime();
        this.endTime = this.getDay(endT);
        //默認查詢最近一周的數據
        this.startTime = new Date(this.endTime).getTime()-3600*24*1000*7;
        this.getData();

 2.時間戳轉化成時間格式

        getFilter(value){
           if(value){
               var oDate = new Date(value);
               return oDate.getFullYear() + '' + (oDate.getMonth() + 1 > 9 ? oDate.getMonth() + 1 : '0' + (oDate.getMonth() + 1)) + '' + (oDate.getDate() > 9 ? oDate.getDate() : '0' + oDate.getDate());
           }
        },
        getDay(value){
           if(value){
               var oDate = new Date(value);
               return oDate.getFullYear() + '-' + (oDate.getMonth() + 1 > 9 ? oDate.getMonth() + 1 : '0' + (oDate.getMonth() + 1)) + '-' + (oDate.getDate() > 9 ? oDate.getDate() : '0' + oDate.getDate());
           }
        }
        date(input) {
            if(input){
                var oDate = new Date(input);
                return oDate.getFullYear() + '-' + (oDate.getMonth() + 1 > 9 ? oDate.getMonth() + 1 : '0' + (oDate.getMonth() + 1)) + '-' + (oDate.getDate() > 9 ? oDate.getDate() : '0' + oDate.getDate()) + ' ' + (oDate.getHours() > 9 ? oDate.getHours() : '0' + oDate.getHours()) + ':' + (oDate.getMinutes() > 9 ? oDate.getMinutes() : '0' + oDate.getMinutes());
            }
        },

3.時間格式轉時間戳

            let startTime = new Date(vm.startTime).getTime();

4.默認從今天起查詢一周的數據(獲取上月的天數)

        //當前13位數的時間戳,去掉時分秒,即為當天零點時間戳
        let nowT = new Date();
        let endT = new Date(nowT).getTime();
        this.timer2 = this.getDay(endT);
        //默認查詢最近一月的數據
        let year = nowT.getFullYear();
        let month = nowT.getMonth();
        let lastMonth = new Date(year, month, 0);
        lastMonth.setMonth(lastMonth.getMonth()+1);
        //將當前的日期置為0,
        lastMonth.setDate(0);
        //再獲取天數即取上個月的最后一天的天數
        let days=lastMonth.getDate();
        this.timer1 = new Date(this.timer2).getTime()-3600*24*1000*days;

5.開始時間要小於結束時間,並且結束時間需加1天查詢數據

        let vm = this;
        let startTime = new Date(vm.timer1).getTime();
        let endT= new Date(vm.timer2).getTime();
        if(!vm.timer1||(!vm.timer2)){
            Message('請選擇開始時間和結束時間');
            return;
        }else{  
            if(startTime>endT){
                Message('開始時間不能大於結束時間');
                return;
            }
        }
        //結束時間多加一天
        let endTime = endT + 3600*24*1000;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2024 CODEPRJ.COM