封装一个day.js设置相对时间

在组件的utils的文件夹中创建一个dateFilter.js文件在这里插入代码片
npm i dayjs --save

import dayjs from 'dayjs'
// 或者 CommonJS
// var dayjs = require('dayjs');

// 引入中文语言包
import 'dayjs/locale/zh-cn'

// 引入插件 dayjs/plugin/relativeTime
// 在你npm i daysj时,插件已经下载了。
// 具备计算相对时间的功能
import rTime from 'dayjs/plugin/relativeTime'
// 使用中文语言包。固定格式
dayjs.locale('zh-cn')

// 使用插件。固定格式dayjs.extend(插件)
dayjs.extend(rTime)
export const relativeTime = function (yourTime) {
  // 使用dayjs提供的api对用户传入的时间 yourTime
  // 进行格式化,以返回一个相对时间
  return dayjs().to(dayjs(yourTime))
  // console.log(yourTime)
  // return dayjs(yourTime).format('DD/MM/YYYY')
}
//格式化时间,格式为:2018-05-08 12:12:12
import dayjs from 'dayjs'
// 或者 CommonJS
// var dayjs = require('dayjs');
export const relativeTime = function (yourTime) {
  // 使用dayjs提供的api对用户传入的时间 yourTime
  // 进行格式化,以返回一个相对时间
return day(youTime).format('YYYY-MM-DD hh:mm:ss')
}

你可能感兴趣的:(前端Vue的学习)