js时间时间戳转换,url转码解码 el-date-picker使用 element confirm 封装

/**
 * 时间戳 转 时间
 * @param time 时间戳 秒
 * @returns  时间 展示
 */
 export const dateFormat = (time: string | number) => {
  if (time === '0' || time === 0 || !time) return '--'
  const timer = new Date(Number(time) * 1000)
  const Y = timer.getFullYear()
  const M = timer.getMonth() + 1 >= 10 ? timer.getMonth() + 1 : '0' + (timer.getMonth() + 1)
  const D = timer.getDate() >= 10 ? timer.getDate() : '0' + timer.getDate()
  const h = timer.getHours() >= 10 ? timer.getHours() : '0' + timer.getHours()
  const m = timer.getMinutes() >= 10 ? timer.getMinutes() : '0' + timer.getMinutes()
  const s = timer.getSeconds() >= 10 ? timer.getSeconds() : '0' + timer.getSeconds()
  return `${Y}-${M}-${D} ${h}:${m}:${s}`
}

//moment.js
moment(xx时间戳).format('YYYY-MM-DD HH:mm:ss') //时间戳转时间
moment(xx时间).valueOf() //时间转时间戳

new Date('xx时间').getTime() //得到xx时间的时间戳
new Date.parse() //时间转时间戳
+new Date() //时间转时间戳
Date.now() //获取当前时间戳
Date.now().getDay() //获取当前星期几 member类型


//今天/昨天 HH:mm  其他时间 yyyy-MM-dd HH:mm
export function timeShowNew(date){
  let _date = new Date(date);
  let _year = _date.getFullYear();
  let _month = ("0" + (_date.getMonth()+1)).slice(-2);
  let _day = ("0"+_date.getDate()).slice(-2);
  let _hour = ("0"+_date.getHours()).slice(-2);
  let _minute = ("0" + _date.getMinutes()).slice(-2);
  let _second = ("0" + _date.getSeconds()).slice(-2);
  
  let nowTime = new Date();
  let criticality = nowTime.getFullYear() + "-" + (nowTime.getMonth()+1) + "-" + nowTime.getDate() + " 00:00:00";
  let _time = +new Date(criticality);
  if(date >= _time){
      return _hour + ":" + _minute
  }else if(date < _time && date > _time-60*60*24*1000){
      return '昨天  ' + _hour + ":" + _minute
  }else{
      return _year + "-" + _month + "-" + _day + " " + _hour + ":" + _minute + ":" + _second
  }
}



  // 时间转时间戳
  dateFormat(dateStr) {
    const date = new Date(dateStr)
    return date.getTime() / 1000
  },

  // 时间戳转时间
  strFormat(dataStr) {
    var time
    if (dataStr === '0' || !dataStr) {
      return '-'
    }
    if (dataStr.length === 10) {
      time = new Date(dataStr * 1000)
    } else {
      time = new Date(dataStr)
    }
    function timeAdd0(str) {
      if (str < 10) {
        str = '0' + str
      }
      return str
    }
    var y = time.getFullYear()
    var m = time.getMonth() + 1
    var d = time.getDate()
    var h = time.getHours()
    var mm = time.getMinutes()
    var s = time.getSeconds()
    return (
      y +
        '-' +
        timeAdd0(m) +
        '-' +
        timeAdd0(d) +
        ' ' +
        timeAdd0(h) +
        ':' +
        timeAdd0(mm) +
        ':' +
        timeAdd0(s)
    )
  },



  // el-date-picker时间只能选择今天之前的
  pickerOptions0: {
    disabledDate(time) {
      return time.getTime() > Date.now() - 8.64e6
    }
  },


  // element confirm 封装
  openConfirm(text, title = '确认提示', config = {}) {
    return new Promise((resolve, reject) => {
      let confirmButtonLoadingClose = () => {}
      const _config = Object.assign(
        {
          showCancelButton: true,
          closeOnClickModal: false,
          dangerouslyUseHTMLString: true
        },
        config
      )
      let afterCloseResolve = () => {}
      _config.beforeClose = (action, instance, done) => {
        if (config.beforeClose) {
          config.beforeClose(action, instance, () => {})
        }
        if (config.confirmCallBack) {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true
            confirmButtonLoadingClose = () => {
              instance.confirmButtonLoading = false
            }
            config.confirmCallBack({
              confirmButtonLoadingClose,
              close: () =>
                new Promise((resolve, reject) => {
                  done()
                  afterCloseResolve = resolve
                  reject()
                }),
              action
            })
          } else {
            done()
          }
        }
        if (!config.confirmButtonLoading) {
          done()
        }
      }
      delete _config.confirmButtonLoading
      MessageBox.confirm(text, title, _config)
        .then(() => {
          afterCloseResolve()
          resolve()
        })
        .catch(() => {})
    })
  },

 解码 decodeURIComponent()  转码 encodeURIComponent()

el-date-picker使用

<el-form-item label="">
  <el-date-picker
    v-model="dateValue"
    type="daterange"
    start-placeholder="开始时间"
    end-placeholder="结束时间"
    format="yyyy-MM-dd"
    value-format="yyyy-MM-dd"
    :picker-options="$common.pickerOptions0"
    @change="dateChange"
  />
</el-form-item>


dateValue: '',

dateChange(val) {
  if (val) {
    this.formData.itime_start = this.$common.dateFormat(val[0] + ' 00:00:00')
    this.formData.itime_end = this.$common.dateFormat(val[1] + ' 23:59:59')
  } else {
    this.formData.itime_start = ''
    this.formData.itime_end = ''
  }
},

 element confirm 封装

deleteFun(row) {
  let text = ''
  if (row.parent_id == '0') {
      text = '是否确定删除此一级标签<br><p>其下所有二级标签将一起删除,用户不可见该标签;已<br>归类的内容将自动解除此标签。</p>'
  } else{
      text = '是否确定删除此二级标签<br><p>删除后用户不可见该标签;已归类的内容将自动解除此<br>标签。</p>'
  }
  this.$common.openConfirm(
    `${text}`,
    "删除提示",
    {
      type: "warning",
      confirmCallBack: ({ confirmButtonLoadingClose }) => {
        community.tagDelete({ tag_id: row.id }).then((r) => {
          if (r.code === 1) {
            this.$common.notifySuccess("删除标签成功", "已删除该标签数据");
            this.onSubmit();
          } else {
            this.$common.notifyError(r.message);
          }
          confirmButtonLoadingClose();
        });
      },
    }
  );
},

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值