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