当前位置:网站首页>Use day JS let time (displayed as minutes, hours, days, months, and so on)

Use day JS let time (displayed as minutes, hours, days, months, and so on)

2022-07-07 13:55:00 Scarring body

design sketch

 Insert picture description here

Code

utils.ts

By introducing utils.ts

import dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';
import relativeTime from 'dayjs/plugin/relativeTime';
dayjs.extend(relativeTime); //  Relative time 
dayjs.locale('zh-cn'); //  Use localized languages 
//  Need to install cnpm i dayjs --save 
/* * @param time  Time string  * @param friendly  Show xxx Time ago  ( Displayed as a few minutes ago   A few hours ago   A few days ago   A few months ago  ) */
 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

Pages used

import {
    getLastTimeStr} from '../utils/utils'// introduce 
 setup() {
    
const data = reactive({
    

 getLastTimeStr(time: string, friendly: boolean) {
    
      return getLastTimeStr(time, friendly);
    },
})
}

 <div>{
    {
    getLastTimeStr(item.create_at, true)}}</div>

原网站

版权声明
本文为[Scarring body]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071154350072.html