vue中使用moment插件详解

安装

npm install moment

引入 

//在main.js中引入

import moment from "moment";

import 'moment/dist/locale/zh-cn'

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

Vue.prototype.moment = moment;

使用

//当前日期--2024-07-16 10:27:52

this.moment().format('YYYY-MM-DD HH:mm:ss')

// startOf(设置为起始时间)

//年初--2024-01-01 00:00:00

this.moment().startOf('year').format('YYYY-MM-DD HH:mm:ss')

//月初--2024-07-01 00:00:00

this.moment().startOf('month').format('YYYY-MM-DD HH:mm:ss')

//日初--2024-07-16 00:00:00

this.moment().startOf('day').format('YYYY-MM-DD HH:mm:ss')

// add(加时间)

//后一年--2025-07-16 10:27:52

this.moment().add(1, 'years').format('YYYY-MM-DD HH:mm:ss')

//后一月--2024-08-16 10:27:52

this.moment().add(1, 'month').format('YYYY-MM-DD HH:mm:ss')

//后一天--2024-07-17 10:27:52

this.moment().add(1, 'days').format('YYYY-MM-DD HH:mm:ss')

//后一小时--2025-07-16 11:27:52

this.moment().add(1, 'hours').format('YYYY-MM-DD HH:mm:ss')

// subtract(减时间)

//前一年--2023-07-16 10:27:52

this.moment().subtract(1, 'years').format('YYYY-MM-DD HH:mm:ss')

//前一月--2024-06-16 10:27:52

this.moment().subtract(1, 'month').format('YYYY-MM-DD HH:mm:ss')

//前一天--2024-07-15 10:27:52

this.moment().subtract(1, 'days').format('YYYY-MM-DD HH:mm:ss')

//前一小时--2024-07-16 09:27:52

this.moment().subtract(1, 'hours').format('YYYY-MM-DD HH:mm:ss')

// weekday(语言包规定的工作日)

//0为周一,1为周二,以此类推,-1为上周日,7为下周日

//周一--2024-07-15 10:27:52

this.moment().weekday(0).format('YYYY-MM-DD HH:mm:ss')

//上周日--2024-07-14 10:27:52

this.moment().weekday(-1).format('YYYY-MM-DD HH:mm:ss')

//下周日--2024-07-22 10:27:52

this.moment().weekday(7).format('YYYY-MM-DD HH:mm:ss')

//官网一些格式化日期

//七月 16日 2024, 10:49:05 上午

this.moment().format('MMMM Do YYYY, h:mm:ss a')

//星期二

this.moment().format('dddd')

//7月 16日 24

this.moment().format("MMM Do YY")

//2024 escaped 七月

this.moment().format('YYYY [escaped] MMMM')

//2024-07-16T10:49:05+08:00

this.moment().format()

相对时间

//16年前

this.moment("20080808", "YYYYMMDD").fromNow()

日历时间

//2024/07/06

this.moment().subtract(10, 'days').calendar()

//上周三11:12

this.moment().subtract(6, 'days').calendar()

//昨天11:12

this.moment().subtract(1, 'days').calendar()

//今天11:12

this.moment().calendar()

//明天11:12

this.moment().add(1, 'days').calendar()

//本周五11:12

this.moment().add(3, 'days').calendar()

//2024/07/26

this.moment().add(10, 'days').calendar()

多语言支持

//zh-cn

this.moment().locale()

//11:17

this.moment().format('LT')

//11:17:19

this.moment().format('LTS')

//2024/07/16

this.moment().format('L')

//2024/7/16

this.moment().format('l')

//2024年7月16日

this.moment().format('LL')

this.moment().format('ll')

//2024年7月16日上午11点17分

this.moment().format('LLL')

//2024年7月16日 11:17

this.moment().format('lll')

//2024年7月16日星期二上午11点17分

this.moment().format('LLLL')

//2024年7月16日星期二 11:17

this.moment().format('llll')

其他

 其他更多设置可以移至官网查看:Moment.js 中文网

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值