element ui 时间格式化

ement ui中表格中有个列属性,用来格式化数据

1
<el-table-column prop="createTime" label="创建时间" :formatter="dateFormat" sortable />
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  // 时间格式化
dateFormat: function(row) {
var t = new Date(row.createTime)// row 表示一行数据, createTime 表示要格式化的字段名称
if(!t){
return ''
}
let year = t.getFullYear()
let month = this.dateIfAddZero(t.getMonth()+1)
let day = this.dateIfAddZero(t.getDate())
let hours = this.dateIfAddZero(t.getHours())
let minutes = this.dateIfAddZero(t.getMinutes())
let seconds = this.dateIfAddZero(t.getSeconds())
return year + '-' + month + '-' + day+ ' ' + hours + ':' + minutes+ ':' + seconds
},
dateIfAddZero : function (time) {
return time < 10 ? '0'+ time : time
}