封装函数:按照需要格式格式化时间
背景:
后端返回的时间是时间对象,格式不是yy-mm-dd hhss或者yy-mm-dd hh-mm-ss时间格式的,所以前端要对拿到的数据做一些处理才可以展示到页面上。这里将处理方式记录下来,方便以后直接拿来使用。如下:
// 格式化时间,一位数不足两位
format(t) {
if (t < 10) {
return '0' + t
} else {
return t
}
},
// 处理时间
getTime (t) {
// 处理当前时间时可以直接const d = new Date()
const d = new Date(t)
const year = d.getFullYear()
const month = this.change(d.getMonth() + 1)
const day = this.change(d.getDate())
const hour = this.change(d.getHours())
const minute = this.change(d.getMinutes())
const second = this.change(d.getSeconds())
//yy-mm-dd hh:mm:ss
// const time = year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second;
const time = String(year) + String(month) + String(day) + String(hour) + String(minute) + String(second)
return time
},
若后端返回的字段是updatetime,遍历数据时就可以使用updatetime = this.getTime(updatetime )
还没有评论,来说两句吧...