Skip to content

Commit ee5d281

Browse files
author
Chu Fan
committed
docs(dayjs): 新增一些文档
1 parent 3dca375 commit ee5d281

10 files changed

Lines changed: 392 additions & 192 deletions

File tree

code/node/dayjs/demo-1.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* dayjs 解析操作
3+
*/
4+
5+
const dayjs = require('dayjs')
6+
7+
console.log(dayjs().format())
8+
// 输出: 2023-03-13T22:28:34+08:00
9+
10+
// 解析
11+
console.log(dayjs('2018-04-04T16:00:00.000Z'))
12+
// 输出:
13+
// {
14+
// '$L': 'en',
15+
// '$d': 2018-04-04T16:00:00.000Z,
16+
// '$x': {},
17+
// '$y': 2018,
18+
// '$M': 3,
19+
// '$D': 5,
20+
// '$W': 4,
21+
// '$H': 0,
22+
// '$m': 0,
23+
// '$s': 0,
24+
// '$ms': 0
25+
// }
26+
27+
// 解析毫秒级时间戳,返回解析后的对象
28+
console.log(dayjs(1318781876406))
29+
30+
// 解析秒级时间戳,返回解析后的对象
31+
console.log(dayjs.unix(1318781876))
32+
console.log(dayjs.unix(1318781876.721))
33+
34+
35+
// Date对象
36+
const date = new Date(2022, 8, 18)
37+
console.log(dayjs(date))
38+
39+
40+
// 对象复制
41+
const a = dayjs()
42+
const b = a.clone()
43+
console.log(a, b)
44+
// a 和 b 是两个独立的 Day.js 对象
45+
// 在 dayjs() 里传入一个 Day.js 对象也会返回一个复制的对象。
46+
const c = dayjs()
47+
const d = dayjs(c)
48+
console.log(c, d)
49+
50+
51+
// 校验是否为日期
52+
console.log(dayjs('2022-01-33').isValid())
53+
// true, parsed to 2022-02-02
54+
console.log(dayjs('some invalid string').isValid())
55+
// false
56+

code/node/dayjs/demo-2.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* dayjs 的取值|赋值操作
3+
*/
4+
const dayjs = require('dayjs')
5+
6+
// 获取或设置毫秒
7+
console.log(dayjs().millisecond())
8+
console.log(dayjs().millisecond(1))
9+
10+
// 获取或设置秒
11+
console.log(dayjs().second())
12+
console.log(dayjs().second(1))
13+
14+
// 获取或设置小时
15+
console.log(dayjs().hour())
16+
console.log(dayjs().hour(12))
17+
18+
/**
19+
* dayjs.date 是该月的日期。 dayjs.day 是星期几
20+
*/
21+
22+
// 获取或设置月份里的日期
23+
console.log(dayjs().date())
24+
console.log(dayjs().date(10))
25+
26+
// 获取或设置星期几
27+
console.log(dayjs().day())
28+
console.log(dayjs().day(3))
29+
30+
31+
// 获取或设置月份
32+
console.log(dayjs().month())
33+
console.log(dayjs().month(0))
34+
35+
// 获取或设置年份
36+
console.log(dayjs().year())
37+
console.log(dayjs().year(2023))
38+
39+
40+
// get方法获取
41+
console.log(dayjs().get('year'))
42+
console.log(dayjs().get('month'))
43+
console.log(dayjs().get('date'))
44+
console.log(dayjs().get('hour'))
45+
console.log(dayjs().get('minute'))
46+
console.log(dayjs().get('second'))
47+
console.log(dayjs().get('millisecond'))
48+
49+
// set方法设置
50+
console.log(dayjs().set('date', 1))
51+
console.log(dayjs().set('month', 6)) // 5月
52+
console.log(dayjs().set('second', 30))
53+
// 支持链式调用
54+
console.log(dayjs().set('hour', 8).set('minute', 35).set('second', 15))

code/node/dayjs/demo-3.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* 操作dayjs对象
3+
*/
4+
const dayjs = require('dayjs')
5+
6+
// 支持链式调用
7+
console.log(dayjs('2019-01-25').add(1, 'day').subtract(1, 'year').year(2009).toString())
8+
9+
// 增加一定时间 参考:https://dayjs.gitee.io/docs/zh-CN/manipulate/add
10+
console.log(dayjs().add(7, 'day'))
11+
console.log(dayjs().add(10, 'hour'))
12+
13+
// 减去一定时间 , 同上
14+
console.log(dayjs().subtract(7, 'year'))
15+
console.log(dayjs().subtract(2, 'month'))
16+
17+
18+
// 设置到一个时间的开始
19+
console.log(dayjs().startOf('year'))
20+
// 设置到一个时间的末尾
21+
console.log(dayjs().endOf('month'))

code/node/dayjs/demo-4.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 展示 Day.js 对象的操作
3+
* api参考:https://dayjs.gitee.io/docs/zh-CN/display/format
4+
*/
5+
6+
const dayjs = require('dayjs')
7+
8+
// 将字符串格式化为日期
9+
console.log(dayjs().format('YYYY-MM-DD HH:mm:ss'))
10+
// 默认返回的是 ISO8601 格式字符串 '2020-04-02T08:02:17-05:00'
11+
console.log(dayjs('2019-01-25').format('[YYYYescape] YYYY-MM-DDTHH:mm:ssZ[Z]'))
12+
// 'YYYYescape 2019-01-25T00:00:00-02:00Z'
13+
console.log(dayjs('2019-01-25').format('DD/MM/YYYY')) // '25/01/2019'
14+
15+
16+
// 时间戳 毫秒 13位
17+
console.log(dayjs('2022-01-25').valueOf())
18+
// 时间戳 秒 10位
19+
console.log(dayjs('2022-01-25').unix())
20+
21+
// 获取当前月份包含的天数
22+
console.log(dayjs('2019-01-25').daysInMonth())
23+
24+
25+
// 原生Date对象
26+
console.log(dayjs('2019-01-25').toDate())

code/node/dayjs/demo-5.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* dayjs的查询操作
3+
* api参考:https://dayjs.gitee.io/docs/zh-CN/query/query
4+
*/
5+
6+
const dayjs = require('dayjs')
7+
8+
9+
// 是否在一个日期之前
10+
console.log(dayjs().isBefore(dayjs('2021-01-01')))
11+
// 比较年份
12+
console.log(dayjs().isBefore('2024-01-01', 'year'))
13+
14+
// 日期是否相同
15+
console.log(dayjs().isSame(dayjs('2011-01-01')))
16+
console.log(dayjs().isSame('2023-3-13', 'year'))
17+
18+
// 是否在一个日期之后面
19+
console.log(dayjs().isAfter(dayjs('2021-01-01')))
20+
// 比较年份
21+
console.log(dayjs().isAfter('2024-01-01', 'year'))
22+
23+
24+
// 是否为dayjs对象
25+
console.log(dayjs.isDayjs(dayjs()))
26+
console.log(dayjs.isDayjs(new Date()))

docs/manuscript/read-books/readme.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@ title: 读书整理
44

55

66

7-
## 技术类
7+
### 技术类
8+
9+
10+
|书名|在线阅读|进度|
11+
|:-:|:-:|:-:|
12+
| ![](https://es6.ruanyifeng.com/images/cover-3rd.jpg "ES6标准入门 " =100x150) | <https://es6.ruanyifeng.com/> | [ x ] |
13+
| ![](https://es6.ruanyifeng.com/images/cover-3rd.jpg "ES6标准入门 " =200x300) | <https://es6.ruanyifeng.com/> | [ x ] |
14+
15+
816

9-
#### ES6标准入门
1017

1118

1219

@@ -22,7 +29,7 @@ title: 读书整理
2229

2330
...
2431

25-
## 非技术类
32+
### 非技术类
2633

2734

2835

docs/manuscript/server-end/node-learn/npm-package.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,5 @@ const readFileAsync = Promise.promisify(fs.readFile)
171171
参考资料:
172172

173173
- 地址:<http://bluebirdjs.com/docs/getting-started.html>
174+
175+

0 commit comments

Comments
 (0)