时间函数

获取N天前或后的时间

//获取当天、7天、30天开始时间that.getDay(7)
    getDay(day){    
      var today = new Date();
      var targetday_milliseconds=today.getTime() + 1000*60*60*24*day;
      today.setTime(targetday_milliseconds); 
      var tYear = today.getFullYear();    
      var tMonth = today.getMonth();    
      var tDate = today.getDate();    
      tMonth = this.doHandleMonth(tMonth + 1);    
      tDate = this.doHandleMonth(tDate);    
      return tYear+"-"+tMonth+"-"+tDate;    
    },    
    doHandleMonth(month) {    
      var m = month;    
      if(month.toString().length == 1){    
        m = "0" + month;    
      }    
      return m;    
    }

开始时间不能大于结束时间

 if (new Date(that.starTime).getTime()>new Date(that.endTime).getTime()) {
        that.$toast.failed("开始时间不能大于结束时间");
        return
      }

20200923111500转成年月日十分秒格式

var str = 20200923111500
str.replace(
              /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/g,
              "$1年$2月$3日 $4时$5分$6秒"
            )

N分钟后的时间

getEndtime(startTime) {  
      let temp = startTime.replace(/-/g, "/")    //ios兼容问题     
      let startDate = new Date(temp); //开始时间,需传入时间参数
      let endDate = new Date();  //结束时间,当前时间
      let t = 900000-(endDate.getTime() - startDate.getTime());  //15分钟时间差的毫秒数
      let d = 0, h = 0, m = 0, s = 0;
      if( t >= 0 ) {
        d = Math.floor( t/1000/3600/24 );
        h = Math.floor(t/1000/60/60%24)<10?'0'+Math.floor(t/1000/60/60%24):Math.floor(t/1000/60/60%24);
        m = Math.floor(t/1000/60%60)<10?'0'+Math.floor(t/1000/60%60):Math.floor(t/1000/60%60);
        s = Math.floor(t/1000%60)<10?'0'+Math.floor(t/1000%60):Math.floor(t/1000%60);
      }
      
      return `${h}:${m}:${s}`
      
    },
//实时倒计时
that.interval  = setInterval(() => {
              that.$set(item, 'countdown', that.getEndtime(item.orders.create_date))
              if (item.countdown=='00:00:00') {
                item.orders.status = '已关闭'
                clearInterval(that.interval)
              }
            }, 1000)   
//离开页面时清除计时器
destroyed() {      
    clearInterval(this.interval)//清除计时器
  },

你可能感兴趣的:(时间函数)