Element UI 时间选中格式化设置

使用element ui 时间选中组件 返回的是个数组,且格式不是我们需要的字符型格式,需要转换为正常的日期时间格式。

如:2020-12-15 10:12:10

    /**
     * 时间切换改变
     *  @newv 时间标签名
     *  @type 类型
     */
    getChangeTimers(newv) {
     if(newv&&newv.length>0){
      const d = new Date(newv[0])
      let hour = d.getHours()
      hour = hour >= 10 ? hour : '0' + hour
      let minute = d.getMinutes()
      minute = minute >= 10 ? minute : '0' + minute
      let second = d.getSeconds()
      second = second >= 10 ? second : '0' + second
      const start = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + hour + ':' + minute + ':' + second
      const d_end = new Date(newv[1])
      let hour_e = d_end.getHours()
      hour_e = hour_e >= 10 ? hour_e : '0' + hour_e
      let minute_e = d_end.getMinutes()
      minute_e = minute_e >= 10 ? minute_e : '0' + minute_e
      let second_e = d_end.getSeconds()
      second_e = second_e >= 10 ? second_e : '0' + second_e
      const ends = d_end.getFullYear() + '-' + (d_end.getMonth() + 1) + '-' + d_end.getDate() + ' ' + hour_e + ':' + minute_e + ':' + second_e

      this.formSearchParams.start = start
      this.formSearchParams.end = ends
     }
    },

使用:

                                                                                                                                                                                          

js调用:

create(){
    this.getChangeTimers(this.searchData)
},
watch: {
    'searchDate'(newv) {
      this.getChangeTimers(newv)
    }
}

二、获取当前时间,且格式化时间;

   /**
     *获取当前时间
     *format=1精确到天
     *format=2精确到分
    */
    getCurrentDate(format) {
      var now = new Date()
      var year = now.getFullYear() // 得到年份
      var month = now.getMonth()// 得到月份
      var date = now.getDate()// 得到日期
      var hour = now.getHours()// 得到小时
      var minu = now.getMinutes()// 得到分钟
      var sec = now.getSeconds()// 得到秒
      month = month + 1
      if (month < 10) month = '0' + month
      if (date < 10) date = '0' + date
      if (hour < 10) hour = '0' + hour
      if (minu < 10) minu = '0' + minu
      if (sec < 10) sec = '0' + sec
      var time = ''
      // 精确到天
      if (format === 1) {
        time = year + '-' + month + '-' + date
      } else if (format === 2) { // 精确到分
        time = year + '-' + month + '-' + date + ' ' + hour + ':' + minu + ':' + sec
      }
      return time
    },
    // 输入结果查看
    console.log(this.getCurrentDate(2))

三,过滤显示日期格式: 如:2021-10-12;

filters: {
    formatData(time, type) {
      if(time && time!='0000-00-00 00:00:00') {
        if (typeof time === "string") {
          var mts = time.match(/(\/Date(\d+)(\d+)\/)/);
          if (mts && mts.length >= 3) {
            time = parseInt(mts[2]);
          }
        }
        time = new Date(time);
        const month = time.getMonth() + 1 > 10
                ? time.getMonth() + 1
                : "0" + (time.getMonth() + 1);
        const day = time.getDate() > 10 ? time.getDate() : "0" + time.getDate();
        return time.getFullYear() + type + month + type + day;
      } else {
        return ''
      }
    }
  },

// 使用 : {
    { times |  formatData('-')}}

 

你可能感兴趣的:(Element,UI)