Skip to content

Commit adef2a2

Browse files
committed
fix(add endof): add endOf && add startOf day
1 parent d41ecd0 commit adef2a2

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

src/index.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,32 @@ class Dayjs {
5757
return this.$date.toUTCString()
5858
}
5959

60-
startOf(arg) {
60+
startOf(arg, isStartOf = true) {
6161
switch (arg) {
6262
case 'year':
63-
return new Dayjs(new Date(this.year(), 0, 1))
63+
if (isStartOf) {
64+
return new Dayjs(new Date(this.year(), 0, 1))
65+
}
66+
return new Dayjs(new Date(this.year(), 11, 31)).endOf('day')
6467
case 'month':
65-
return new Dayjs(new Date(this.year(), this.month(), 1))
68+
if (isStartOf) {
69+
return new Dayjs(new Date(this.year(), this.month(), 1))
70+
}
71+
return new Dayjs(new Date(this.year(), this.month() + 1, 0)).endOf('day')
72+
case 'day':
73+
if (isStartOf) {
74+
return new Dayjs(this.toDate().setHours(0, 0, 0, 0))
75+
}
76+
return new Dayjs(this.toDate().setHours(23, 59, 59, 999))
6677
default:
67-
return this
78+
return this.clone()
6879
}
6980
}
7081

82+
endOf(arg) {
83+
return this.startOf(arg, false)
84+
}
85+
7186
set(string, int) {
7287
if (!Utils.isNumber(int)) return this
7388
switch (string) {

test/manipulate.test.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,24 @@ afterEach(() => {
1010
MockDate.reset()
1111
})
1212

13-
it('StartOf Year', () => {
13+
it('StartOf EndOf Year', () => {
1414
expect(dayjs().startOf('year').unix()).toBe(moment().startOf('year').unix())
15+
expect(dayjs().endOf('year').unix()).toBe(moment().endOf('year').unix())
1516
})
1617

17-
it('StartOf Month', () => {
18+
it('StartOf EndOf Month', () => {
1819
expect(dayjs().startOf('month').unix()).toBe(moment().startOf('month').unix())
20+
expect(dayjs().endOf('month').unix()).toBe(moment().endOf('month').unix())
1921
})
2022

21-
it('StartOf Other', () => {
23+
it('StartOf EndOf Day', () => {
24+
expect(dayjs().startOf('day').unix()).toBe(moment().startOf('day').unix())
25+
expect(dayjs().endOf('day').unix()).toBe(moment().endOf('day').unix())
26+
})
27+
28+
it('StartOf EndOf Other -> no change', () => {
2229
expect(dayjs().startOf('otherString').unix()).toBe(moment().startOf('otherString').unix())
30+
expect(dayjs().endOf('otherString').unix()).toBe(moment().endOf('otherString').unix())
2331
})
2432

2533
it('Add Time days', () => {

0 commit comments

Comments
 (0)