vue3使用moment

moment安装

npm install moment

moment 引用

utils.ts文件

import moment from 'moment'
import 'moment/dist/locale/zh-cn'
moment.locale('zh-cn') //中文化

公共方法

utils.ts文件

export const getFullTime = (time?: string, format?: string) => {
  format = format ? format : 'YYYY-MM-DD HH:mm:ss'
  if (time) {
    return moment(time).format(format)
  } else {
    return moment().format(format)
  }
}

使用

import { getFullTime } from '~/src/utils'
getFullTime('', 'YYYY年MM月DD日 HH:mm:ss') // 2024年10月07日 11:42:05

moment方法

 moment().format('YYYY年MM月DD日 HH:mm:ss');  // 2024年02月05日 11:42:05
moment().format('MMMM Do YYYY, h:mm:ss a');  // 二月 5日 2024, 11:42:57 中午
moment().format('dddd');                     // 星期一
moment().format("MMM Do YY");                // 2月 5日 24
moment("20140709", "YYYYMMDD").fromNow();    // 10 年前
moment().startOf('day').fromNow();           // 12 小时前
moment().startOf('hour').fromNow();          // 45 分钟前
moment().subtract(1, 'days').calendar();     // 昨天11:45
moment().add(1, 'days').calendar();          // 明天11:45
moment().format('LT');   // 11:49
moment().format('LTS');  // 11:49:30
moment().format('L');    // 2024/02/05
moment().format('l');    // 2024/02/05
moment().format('LL');   // 2024年02月05日
moment().format('ll');   // 2024年02月05日
moment().format('LLL');  // 2024年02月05日下午1点05分
moment().format('lll');  // 2024年02月05日 13:05
moment().format('LLLL'); // 2024年02月05日星期一下午1点05分
moment().format('llll'); // 2024年02月05日星期一 13:05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值