前端使用dayjs转换时间格式

643 阅读1分钟

首先用npm下载dayjs

// 下载 dayjs
npm install dayjs

2.在main.js中全局注册包

import dayjs from 'dayjs'

Vue.prototype.dayjs = dayjs

3.封装的函数可以在main.js中全局定义,也可以在需要使用它的组件中定义,现在我写在当前组件中:

// 形参 data 就是传入的想要转换的时间格式
time (data) {

return this.dayjs(data).format('YYY-MM-DD HH:mm:ss')

  }

4.在需要用到它的组件中使用它:

<template slot-scope="{row}">

{{ time(row.addDate) }} 

</template>