首页 前端知识 vue中使用moment插件详解

vue中使用moment插件详解

2024-09-09 00:09:27 前端知识 前端哥 658 636 我要收藏

安装

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 中文网

转载请注明出处或者链接地址:https://www.qianduange.cn//article/17959.html
标签
评论
发布的文章

javascript jQuery

2024-09-21 22:09:33

【若依】表格固定列宽

2024-09-21 22:09:30

Knockout-jQueryUI 使用指南

2024-09-21 22:09:29

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!