时间数据2020-09-29T18:02:02.000Z转化为‘YYYY-MM-DD HH:mm:ss‘

 一.目标

把时间转换成'YYYY-MM-DD HH:mm:ss'

查找工具

dayjs第三方库: https://dayjs.fenxianglu.cn/

下载dayjs到工程中      yarn add dayjs

工具函数

因为以后可能也要处理时间, 所以封装个工具函数

在main.js文件中全局注册

import dayjs from 'dayjs'
Vue.prototype.dayjs = dayjs

我这里直接在main.js文件中注册,所以函数直接封装在这里了,大家可以封装一个专门的组件来使用

timeChange(data) {
      
      let time = data //将需要格式化的数据传入
      time = this.dayjs(time).format('YYYY-MM-DD HH:mm:ss')
      return time 

}

然后调用该函数,参数是当前需要格式化的时间数据

你可能感兴趣的:(javascript)