0
点赞
收藏
分享

微信扫一扫

element ui时间格式转换

林肯公园_97cc 2022-04-21 阅读 99

转换时间格式

做项目时候遇到的问题整理一下
前端获取到的时间格式是这样的
在这里插入图片描述

但后端需要接受是YY-mm-DD HH-mm-ss 这种格式就需要转一下时间的格式
话不多说上代码,

format (date, type) {
      let yy = new Date(date).getFullYear()
      let MM = this.addZero(new Date(date).getMonth() + 1)
      let dd = this.addZero(new Date(date).getDate())
      let HH = this.addZero(new Date(date).getHours())
      let mm = this.addZero(new Date(date).getMinutes())
      let ss = this.addZero(new Date(date).getSeconds())
      if (type === 'yy') {
        return yy.toString()
      } else if (type === 'yy-MM-dd HH:mm:ss') {
        return yy + '-' + MM + '-' + dd + ' ' + HH + ':' + mm + ':' + ss
      }
    },
    addZero (data) {
      if (data < 10) {
        return '0' + data
      } else {
        return data
      }
    },

addZero 方法是补零,format方法里输出两种格式YY或YY-MM-DD HH-mm-ss,用的时候直接传时间

 this.format(this.formItem.yearStart, 'yy')
this.format(this.formItem.getDate, 'yy-MM-dd HH:mm:ss')
举报

相关推荐

0 条评论