Vue 中 day.js的基本使用

​// npm i dayjs  下载dayjs 

// 引入 Vue
import Vue from 'vue';

// 引入 day.js
import dayjs from 'dayjs';

// 引入语言配置
import 'dayjs/locale/zh-cn';

// 全局使用语言配置
dayjs.locale('zh-cn');

// 相对时间
// RelativeTime 增加了 .from .to .fromNow .toNow 4 个 API 来展示相对的时间 (e.g. 3 小时以前).

// 引入相对时间插件
var relativeTime = require('dayjs/plugin/relativeTime')
dayjs.extend(relativeTime)

// 距离 X 的相对时间 .from(compared: Dayjs, withoutSuffix?: boolean)
// 返回 string 距离 X 的相对时间

dayjs().from(dayjs('1990-01-01')) // 31 年后
dayjs().from(dayjs('1990-01-01'), true) // 31 年


// 距离现在的相对时间 .fromNow(withoutSuffix?: boolean)
// 返回 string 距离现在的相对时间

dayjs().fromNow()


// 到 X 的相对时间 .to(compared: Dayjs, withoutSuffix?: boolean)
// 返回 string 到 X 的相对时间

dayjs().to(dayjs('1990-01-01')) // 31 年前


// 到现在的相对时间 .toNow(withoutSuffix?: boolean)
// 返回 string 到现在的相对时间

dayjs().toNow()



// 注册为全局过滤器
Vue.filter('dateFormat', val => {
    return dayjs(val).format('YYYY-MM-DD HH:mm:ss');
});

Vue.filter("dateFrom", val => {
  return dayjs().from(dayjs(val))
});

........

​


版权声明:本文为qq_25874923原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。