当前位置:网站首页>使用day.js让时间 (显示为几分钟前 几小时前 几天前 几个月前 )
使用day.js让时间 (显示为几分钟前 几小时前 几天前 几个月前 )
2022-07-07 11:54:00 【疤痕体】
效果图
代码
utils.ts
通过引入utils.ts
import dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';
import relativeTime from 'dayjs/plugin/relativeTime';
dayjs.extend(relativeTime); // 相对时间
dayjs.locale('zh-cn'); // 使用本地化语言
// 需要安装cnpm i dayjs --save
/* * @param time 时间字符串 * @param friendly 显示xxx时间前 (显示为几分钟前 几小时前 几天前 几个月前 ) */
export function getLastTimeStr(time: string, friendly: boolean) {
if (friendly) {
return dayjs(time).fromNow().replace(' ', '');
}
return dayjs(new Date(time)).format('YYYY-MM-DD HH:mm');
}
main.js
import dayjs from 'dayjs'
app.config.globalProperties.day=dayjs
使用的页面
import {
getLastTimeStr} from '../utils/utils'//引入
setup() {
const data = reactive({
getLastTimeStr(time: string, friendly: boolean) {
return getLastTimeStr(time, friendly);
},
})
}
<div>{
{
getLastTimeStr(item.create_at, true)}}</div>
边栏推荐
- Solve the cache breakdown problem
- 2022-7-7 Leetcode 34. Find the first and last positions of elements in a sorted array
- Cinnamon taskbar speed
- Ways to improve the performance of raspberry pie
- 社会责任·价值共创,中关村网络安全与信息化产业联盟对话网信企业家海泰方圆董事长姜海舟先生
- Redis can only cache? Too out!
- 提升树莓派性能的方法
- 【日常训练】648. 单词替换
- 干货|总结那些漏洞工具的联动使用
- 2022-7-7 Leetcode 34.在排序数组中查找元素的第一个和最后一个位置
猜你喜欢
室内ROS机器人导航调试记录(膨胀半径的选取经验)
"Song of ice and fire" in the eleventh issue of "open source Roundtable" -- how to balance the natural contradiction between open source and security?
"New red flag Cup" desktop application creativity competition 2022
Help tenants
Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
566. Reshaping the matrix
1、深拷贝 2、call apply bind 3、for of for in 区别
TPG x AIDU | AI leading talent recruitment plan in progress!
cmake 学习使用笔记(一)
Build a secure and trusted computing platform based on Kunpeng's native security
随机推荐
2022-7-7 Leetcode 844. Compare strings with backspace
118. Yanghui triangle
如何让join跑得更快?
Write it down once Net a new energy system thread surge analysis
AI人才培育新思路,这场直播有你关心的
Final review notes of single chip microcomputer principle
一文读懂数仓中的pg_stat
DID登陆-MetaMask
My "troublesome" subordinates after 00: not bad for money, against leaders, and resist overtime
[fortress machine] what is the difference between cloud fortress machine and ordinary fortress machine?
.net core 关于redis的pipeline以及事务
Laravel Form-builder使用
Mongodb replication (replica set) summary
Custom thread pool rejection policy
LED light of single chip microcomputer learning notes
Leetcode simple question sharing (20)
Excerpt from "misogyny: female disgust in Japan"
2022-7-7 Leetcode 34.在排序数组中查找元素的第一个和最后一个位置
数据库系统概论-第一章绪论【概念模型、层次模型和三级模式(外模式、模式、内模式)】
Oracle advanced (V) schema solution