Day.js和Moment.js的常用方法

木头的喵喵拖孩

项目中经常会使用 Day.js 和 Moment.js来操作日期时间,但老是忘记常用方法,去官网查 API,有时候官网打不开或者卡的要死,所以写了个简单的笔记。

安装和引入

1
2
# Day.js
npm install dayjs
1
2
// Day.js
let dayjs = require("dayjs");
1
2
# Moment.js
npm install moment
1
2
// Moment.js
let moment = require("moment");

解析

所谓解析,就是一众不是 Day.js/Moment.js 对象的数据,通过 Day.js/Moment.js 解析成 Day.js/Moment.js 对象。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Day.js
dayjs(); // 当前时间
dayjs("2018-04-04T16:00:00.000Z"); // ISO 8601格式字符串
dayjs("2018-04-04"); // ISO 8601格式字符串
dayjs.extend(customParseFormat);
dayjs("1995-12-25 12:29:03.329", "YYYY-MM-DD HH:mm:ss.SSS"); // 自定义格式字符串:年-月-日 时:分:秒.毫秒
dayjs(new Date()); // Date对象

// Moment.js
moment(); // 当前时间
moment("2018-04-04T16:00:00.000Z"); // ISO 8601格式字符串
moment("2018-04-04");
moment("1995-12-25 12:29:03.329", "YYYY-MM-DD HH:mm:ss.SSS"); // 自定义格式字符串:年-月-日 时:分:秒.毫秒
moment(new Date()); // Date对象

展示

所谓展示,就是 Day.js/Moment.js 对象,通过 Day.js/Moment.js 转换成格式化的日期时间字符串。

1
2
3
4
5
// Day.js
dayjs().format("YYYY-MM-DD HH:mm:ss.SSS d"); // 输出:2024-07-19 15:16:26.000 3(最后这个3代表周几,周日为0,周六为6)

// Moment.js
moment().format("YYYY-MM-DD HH:mm:ss.SSS d"); // 输出:2024-07-19 15:16:26.000 3(最后这个3代表周几,周日为0,周六为6)

计算

所谓计算,就是 Day.js/Moment.js 对象,通过 Day.js/Moment.js 计算得到新的 Day.js/Moment.js 对象。

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
// Day.js
dayjs().add(1, "day"); // 加1天
dayjs().add(1, "week"); // 加1周
dayjs().add(1, "month"); // 加1月(注意月份的范围是0-11)
dayjs().add(1, "year"); // 加1年
dayjs().add(1, "hour"); // 加1小时
dayjs().add(1, "minute"); // 加1分钟
dayjs().add(1, "second"); // 加1秒
dayjs().add(1, "millisecond"); // 加1毫秒

dayjs().subtract(1, "day"); // 减1天
dayjs().subtract(1, "week"); // 减1周
dayjs().subtract(1, "month"); // 减1月(注意月份的范围是0-11)
dayjs().subtract(1, "year"); // 减1年
dayjs().subtract(1, "hour"); // 减1小时
dayjs().subtract(1, "minute"); // 减1分钟
dayjs().subtract(1, "second"); // 减1秒
dayjs().subtract(1, "millisecond"); // 减1毫秒

// Moment.js
moment().add(1, "days"); // 加1天
moment().add(1, "weeks"); // 加1周
moment().add(1, "months"); // 加1月(注意月份的范围是0-11)
moment().add(1, "years"); // 加1年
moment().add(1, "hours"); // 加1小时
moment().add(1, "minutes"); // 加1分钟
moment().add(1, "seconds"); // 加1秒
moment().add(1, "milliseconds"); // 加1毫秒

moment().subtract(1, "days"); // 减1天
moment().subtract(1, "weeks"); // 减1周
moment().subtract(1, "months"); // 减1月(注意月份的范围是0-11)
moment().subtract(1, "years"); // 减1年
moment().subtract(1, "hours"); // 减1小时
moment().subtract(1, "minutes"); // 减1分钟
moment().subtract(1, "seconds"); // 减1秒
moment().subtract(1, "milliseconds"); // 减1毫秒

取值/赋值

所谓取值/赋值,就是 Day.js/Moment.js 对象,通过 Day.js/Moment.js 设置/获取属性。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Day.js
dayjs().year(); // 获取年(参数为空时取值,参数不为空时赋值)
dayjs().month(); // 获取月(参数为空时取值,参数不为空时赋值),传入0到11的 number。 如果超出这个范围,它会进位到年份。
dayjs().date(); // 获取日(参数为空时取值,参数不为空时赋值),接受1到31的数字。 如果超出这个范围,它会进位到月份。
dayjs().day(); // 获取周(参数为空时取值,参数不为空时赋值),传入0到6的 number。 如果超出这个范围,它会进位到其他周。
dayjs().hour(); // 获取时(参数为空时取值,参数不为空时赋值),传入0到23的数字。 如果超出这个范围,它会进位到天数。
dayjs().minute(); // 获取分(参数为空时取值,参数不为空时赋值),传入0到59的数字。 如果超出这个范围,它会进位到小时。
dayjs().second(); // 获取秒(参数为空时取值,参数不为空时赋值),传入0到59的数字。 如果超出这个范围,它会进位到分钟。
dayjs().millisecond(); // 获取毫秒(参数为空时取值,参数不为空时赋值),传入0到999的数字。 如果超出这个范围,它会进位到秒。

// Moment.js
moment().year(); // 获取年(参数为空时取值,参数不为空时赋值)
moment().month(); // 获取月(参数为空时取值,参数不为空时赋值),传入0到11的 number。 如果超出这个范围,它会进位到年份。
moment().date(); // 获取日(参数为空时取值,参数不为空时赋值),接受1到31的数字。 如果超出这个范围,它会进位到月份。
moment().day(); // 获取周(参数为空时取值,参数不为空时赋值),传入0到6的 number。 如果超出这个范围,它会进位到其他周。
moment().hour(); // 获取时(参数为空时取值,参数不为空时赋值),传入0到23的数字。 如果超出这个范围,它会进位到天数。
moment().minute(); // 获取分(参数为空时取值,参数不为空时赋值),传入0到59的数字。 如果超出这个范围,它会进位到小时。
moment().second(); // 获取秒(参数为空时取值,参数不为空时赋值),传入0到59的数字。 如果超出这个范围,它会进位到分钟。
moment().millisecond(); // 获取毫秒(参数为空时取值,参数不为空时赋值),传入0到999的数字。 如果超出这个范围,它会进位到秒。

克隆

所有的 Day.js/Moment.js 对象都是不可变的。 但如果有必要,使用 dayjs#clone 可以复制出一个当前对象。

1
2
3
4
5
// Day.js
dayjs().clone();

// Moment.js
moment().clone();

参考

Day.js
Moment.js

  • 标题: Day.js和Moment.js的常用方法
  • 作者: 木头的喵喵拖孩
  • 创建于: 2024-07-19 15:16:26
  • 更新于: 2024-07-19 16:37:21
  • 链接: https://blog.xx-xx.top/2024/07/19/Day-js和Moment-js的常用方法/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
此页目录
Day.js和Moment.js的常用方法