Moment.js

2023/2/7 前端依赖库

# 1. 链接

# 2. API 使用

# 引入

const moment = require('moment');
moment.locale('zh-cn');
1
2

# 度量单位

快捷键
years y
quarters Q
months M
weeks w
days d
hours h
minutes m
seconds s
milliseconds ms

# 解析

// moment()
moment();
moment(Number);   // 1318781876406
moment(String);   // '2023-03-03'
moment(Number[]); // [2023, 3, 3]
1
2
3
4
5

# 取值/赋值

// get()
moment().get('year');
moment().get('quarters'); // 季度 1 ~ 4
moment().get('month');  // 0 ~ 11
moment().get('weeks'); // 几年第几周
moment().get('date');
moment().get('day'); // 星期日为 0、星期六为 6
moment().get('hour');
moment().get('minute');
moment().get('second');
moment().get('millisecond');
moment().get(unit) === moment()[unit]()

// set()
moment().set('year', 2013);
moment().set('month', 3);  // 四月
moment().set('date', 1);
moment().set('day', 1);
moment().set('hour', 13);
moment().set('minute', 20);
moment().set('second', 30);
moment().set('millisecond', 123);
moment().set({ 'year': 2013, 'month': 3 });
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 操作

// add():通过增加时间来改变原始的 moment
moment().add(Number, String);
moment().add(Object);
moment().add(7, 'days').add(1, 'months');
moment().add({ days: 7, months: 1 });

// subtract():通过减去时间来改变原始的 moment
moment().subtract(Number, String);
moment().subtract(Object);
moment().subtract(7, 'days');

// startOf():通过将原始的 moment 设置为时间单位的开头来对其进行更改
moment().startOf(String);
moment().startOf('year'); // 设置为今年 1 月 1 日 00:00:00.000 --> 2023-01-01 00:00:00 000

// endOf():通过将原始的 moment 设置为时间单位的末尾来对其进行更改
moment().endOf(String);
moment().endOf('year'); // 设置为今年 12 月 31 日 23:59:59.999 --> 2023-12-31 23:59:59 999
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 显示

一旦解析和操作完成后,需要某些方式来显示 moment。

// format()
moment().format();
moment().format(String);
moment().format('YYYY-MM-DD HH:mm:ss')

// fromNow():有时称为时间间隔或相对时间
moment().fromNow();
moment().fromNow(Boolean);
moment([2022, 0, 27]).fromNow();     // 1 年前
moment([2022, 0, 27]).fromNow(true); // 1 年

// from()
moment().from(Moment|String|Number|Date|Array);
moment().from(Moment|String|Number|Date|Array, Boolean);
const a = moment([2007, 0, 28]);
a.from(moment([2007, 0, 29])); // 1 天前
a.from([2007, 0, 29]);         // 1 天前
a.from(new Date(2007, 0, 29)); // 1 天前
a.from('2007-01-29');          // 1 天前
a.from(moment([2007, 0, 29]), true)); // 1 天,将 true 用作第二个参数会返回不带后缀的值

// toNow():有时称为时间间隔或相对时间
moment().toNow();
moment().toNow(Boolean);
moment.fromNow() = - moment.toNow()
moment([2022, 0, 27]).toNow();     // 1 年后
moment([2022, 0, 27]).toNow(true); // 1 年

// to()
moment().to(Moment|String|Number|Date|Array);
moment().to(Moment|String|Number|Date|Array, Boolean);
moment.from() = - moment.to()

// diff()
moment().diff(Moment|String|Number|Date|Array);
moment().diff(Moment|String|Number|Date|Array, String);
moment().diff(Moment|String|Number|Date|Array, String, Boolean);
moment([2023, 0, 29]).diff(moment([2023, 0, 28])); // 86400000
moment([2023, 0, 29]).diff(moment([2023, 0, 28]), 'days'); // 1
moment([2023, 0, 27]).diff(moment([2023, 0, 28]), 'days'); // -1

// valueOf():输出自 Unix 纪元以来的毫秒数
moment().valueOf(); // 1678325839942
+moment();

// unix():输出自 Unix 纪元以来的秒数
moment().unix();    // 1678325899

// daysInMonth()
moment().daysInMonth();
moment([2023, 1]).daysInMonth(); // 28

// toDate():要获取 Moment.js 封装的原生 Date 对象的副本
moment().toDate();
moment([2023, 2, 8]).toDate(); // 2023-03-07T16:00:00.000Z

// toArray()
moment().toArray(); // [2023, 2, 13, 20, 26, 26, 232]

// toObject()
moment().toObject();
// {
//   years: 2023,
//   months: 2,
//   date: 13,
//   hours: 20,
//   minutes: 29,
//   seconds: 49,
//   milliseconds: 788
// }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

# 查询

// isBefore()
moment().isBefore(Moment|String|Number|Date|Array);
moment().isBefore(Moment|String|Number|Date|Array, String); // 将粒度限制为毫秒以外的度量单位
moment([2023, 3, 3]).isBefore(moment([2023, 3, 10])); // true
moment([2023, 3, 3]).isBefore(moment([2023, 3, 10]), 'years'); // false
moment([2023, 3, 3]).isBefore(moment([2023, 3, 10]), 'days');  // true
// 当包含第二个参数时,它将会匹配所有等于或更大的单位
moment([2023, 2, 13]).isBefore(moment([2023, 3, 10]), 'days'); // true

// isSame():当包含第二个参数时,它将会匹配所有等于或更大的单位
moment().isSame(Moment|String|Number|Date|Array);
moment().isSame(Moment|String|Number|Date|Array, String); // 将粒度限制为毫秒以外的度量单位

// isAfter():当包含第二个参数时,它将会匹配所有等于或更大的单位
moment().isAfter(Moment|String|Number|Date|Array);
moment().isAfter(Moment|String|Number|Date|Array, String); // 将粒度限制为毫秒以外的度量单位

// isLeapYear():判断该年是否是闰年
moment().isLeapYear();
moment([2023]).isLeapYear(); // false
moment([2024]).isLeapYear(); // true

// isMoment():要检查变量是否为 moment 对象
moment.isMoment(obj);
moment.isMoment();           // false
moment.isMoment(new Date()); // false
moment.isMoment(moment());   // true

// isDate():要检查变量是否为原生 JS Date 对象
moment.isDate(obj);
moment.isDate();             // false
moment.isDate(new Date());   // true
moment.isDate(moment());     // false
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

# 国际化

// 全局
moment.locale(String); // 'en'、'zh-cn'
moment.locale(String[]);
moment.locale(String, Object);

// 局部
moment().locale(String|Boolean);
moment([2022, 0, 27]).locale('zh-cn').fromNow(); // 1 年前
moment([2022, 0, 27]).locale('en').fromNow();    // a year ago
1
2
3
4
5
6
7
8
9

# 插件

英文文档链接:https://momentjs.com/docs/#/plugins/ (opens new window)

中文文档链接:http://momentjs.cn/docs/#/plugins/ (opens new window)

# 3. 实战使用

Last Updated: 2023/04/22, 23:39:26
彩虹
周杰伦