moment时间格式修改

1. 引入moment.js
npm install moment
2.在main.js中进行使用
import moment from 'moment';
moment.locale('zh-cn');
Vue.prototype.moment = moment;
3.在vue文件中引用(全局引入就不需要单独引入了)
import moment from "moment";
moment使用方法
获取时间
new Date() // Thu Oct 27 2022 18:29:54 GMT+0800 (中国标准时间)
new Date().getTime() //1666866447103
this.moment() // Thu Oct 27 2022 18:11:27 GMT+0800
修改日期格式
this.moment().format('x') // 1666866447103
this.moment().valueOf() // 1666866447103
this.moment().format("yyyy-MM-DDTHH:mm:ss.000+08:00") // 2022-10-27T18:46:19.000+08:00
this.$moment().format("YYYY-MM-DD") + " 00:00:00" // 2022-10-27 00:00:00

将字符串格式化为 ISO8601 标准。

this.moment().toISOString() // 2022-10-28T03:37:45.573Z
令牌 输出
月份 M 1 2 ... 11 12
Mo 1st 2nd ... 11th 12th
MM 01 02 ... 11 12
MMM Jan Feb ... Nov Dec
MMMM January February ... November December
季度 Q 1 2 3 4
Qo 1st 2nd 3rd 4th
月份的日期 D 1 2 ... 30 31
Do 1st 2nd ... 30th 31st
DD 01 02 ... 30 31
年份的日期 DDD 1 2 ... 364 365
DDDo 1st 2nd ... 364th 365th
DDDD 001 002 ... 364 365
星期几 d 0 1 ... 5 6
do 0th 1st ... 5th 6th
dd Su Mo ... Fr Sa
ddd Sun Mon ... Fri Sat
dddd Sunday Monday ... Friday Saturday
星期几(语言环境) e 0 1 ... 5 6
星期几(ISO) E 1 2 ... 6 7
年份的星期 w 1 2 ... 52 53
wo 1st 2nd ... 52nd 53rd
ww 01 02 ... 52 53
年份的星期(ISO) W 1 2 ... 52 53
Wo 1st 2nd ... 52nd 53rd
WW 01 02 ... 52 53
年份 YY 70 71 ... 29 30
YYYY 1970 1971 ... 2029 2030
Y 1970 1971 ... 9999 +10000 +10001 注意:对于 9999 年以后的日期,这符合 ISO 8601 标准。
周年 gg 70 71 ... 29 30
gggg 1970 1971 ... 2029 2030
周年(ISO) GG 70 71 ... 29 30
GGGG 1970 1971 ... 2029 2030
子午线 A AM PM
a am pm
小时 H 0 1 ... 22 23
HH 00 01 ... 22 23
h 1 2 ... 11 12
hh 01 02 ... 11 12
k 1 2 ... 23 24
kk 01 02 ... 23 24
分钟 m 0 1 ... 58 59
mm 00 01 ... 58 59
秒钟 s 0 1 ... 58 59
ss 00 01 ... 58 59
小数秒钟 S 0 1 ... 8 9
SS 00 01 ... 98 99
SSS 000 001 ... 998 999
SSSS ... SSSSSSSSS 000[0..] 001[0..] ... 998[0..] 999[0..]
时区 z or zz EST CST ... MST PST 注意:从 1.6.0 版本开始,z/zz 格式的令牌已从普通的 moment 对象中弃用。 在此处了解更多信息。 但是,如果将特定时区与 moment-timezone 插件一起使用,它们会起作用。
Z -07:00 -06:00 ... +06:00 +07:00
ZZ -0700 -0600 ... +0600 +0700
Unix 时间戳 X 1360013296
Unix 毫秒时间戳 x 1360013296123

默认事件格式:

日期增加
this.moment().add(1, 'days').format('YYYY-MM-DD') //2022-10-28
this.moment().add(1, 'months').format('YYYY-MM-DD') //2022-11-27
this.moment().add(1, 'Q').format('YYYY-MM-DD') //2023-01-28
日期减少
this.moment().subtract(7, 'days').format('yyyy-MM-DD') //2022-10-20

2.12.0 版本开始,当为日期和月份传入小数时,它们会被四舍五入到最接近的整数。 星期、季度、年份会被转换到日期或月份,然后四舍五入到最接近的整数。

日期起点
// 2022-10-28
this.moment().startOf('month').format('yyyy-MM-DD') // 2022-10-01
日期结尾
this.moment().endOf("year").format('yyyy-MM-DD') // 2022-12-31

如果对希望简短,也有一些快捷的键。

例: moment().add(7, 'd');

快捷键
years y
quarters Q
months M
weeks w
days d
hours h
minutes m
seconds s
milliseconds ms
转义字符
this.moment().format('[今天] dddd'); // '今天 Sunday'
比较

可以使用时间戳直接比较大小,也可以利用方法进行比较

检查一个 moment 是否在另一个 moment 之前。 第一个参数会被解析为 moment(如果尚未解析)。

this.moment('2010-10-20').isBefore('2010-10-21'); // true

由于第二个参数用于确定精度,且不仅仅是要检查的单个值,因此使用 day 将会检查年份、月份、日期。

this.moment('2010-10-20').isBefore('2010-12-31', 'year'); // false
this.moment('2010-10-20').isBefore('2011-01-01', 'year'); // true

是否相同

this.moment('2010-10-20').isSame('2009-12-31', 'year');  // false

是否在之后

this.moment('2010-10-20').isAfter('2010-10-19'); // true