Skip to content

Commit 74b0bc1

Browse files
authored
Merge pull request #16 from xx45/dev
fix(date()): add date()
2 parents f65194a + 85a3b76 commit 74b0bc1

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

src/index.js

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ class Dayjs {
99
constructor(config) {
1010
this.utc = false
1111
const args = this.parseConfig(config)
12-
this.date = new Date(args)
13-
this.timeZone = this.date.getTimezoneOffset() / 60
12+
this.$date = new Date(args)
13+
this.timeZone = this.$date.getTimezoneOffset() / 60
1414
this.timeZoneString = padStart(String(this.timeZone * -1).replace(/^(.)?(\d)/, '$10$200'), 5, '+')
15-
this.mYear = this.date.getFullYear()
16-
this.mMonth = this.date.getMonth()
17-
this.mDay = this.date.getDate()
18-
this.mWeek = this.date.getDay()
19-
this.mHour = this.date.getHours()
20-
this.mMinute = this.date.getMinutes()
21-
this.mSecond = this.date.getSeconds()
15+
this.$year = this.$date.getFullYear()
16+
this.$month = this.$date.getMonth()
17+
this.$day = this.$date.getDate()
18+
this.$week = this.$date.getDay()
19+
this.$hour = this.$date.getHours()
20+
this.$minute = this.$date.getMinutes()
21+
this.$second = this.$date.getSeconds()
2222
}
2323

2424
parseConfig(config) {
@@ -35,21 +35,25 @@ class Dayjs {
3535
}
3636

3737
year() {
38-
return this.mYear
38+
return this.$year
3939
}
4040

4141
month() {
42-
return this.mMonth
42+
return this.$month
43+
}
44+
45+
date() {
46+
return this.$day
4347
}
4448

4549
unix() {
4650
// timezone(hour) * 60 * 60 * 1000 => ms
4751
const zonePad = !this.utc ? 0 : this.timeZone * 60 * 60 * 1000
48-
return Math.floor((this.date.getTime() + zonePad) / 1000)
52+
return Math.floor((this.$date.getTime() + zonePad) / 1000)
4953
}
5054

5155
toString() {
52-
return this.date.toUTCString()
56+
return this.$date.toUTCString()
5357
}
5458

5559
startOf(arg) {
@@ -99,33 +103,33 @@ class Dayjs {
99103
return formatStr.replace(/Y{2,4}|M{1,2}|D{1,2}|d{1,4}|H{1,2}|m{1,2}|s{1,2}|Z{1,2}/g, (match) => {
100104
switch (match) {
101105
case 'YY':
102-
return String(this.mYear).slice(-2)
106+
return String(this.$year).slice(-2)
103107
case 'YYYY':
104-
return String(this.mYear)
108+
return String(this.$year)
105109
case 'M':
106-
return String(this.mMonth + 1)
110+
return String(this.$month + 1)
107111
case 'MM':
108-
return padStart(String(this.mMonth + 1), 2, '0')
112+
return padStart(String(this.$month + 1), 2, '0')
109113
case 'D':
110-
return String(this.mDay)
114+
return String(this.$day)
111115
case 'DD':
112-
return padStart(String(this.mDay), 2, '0')
116+
return padStart(String(this.$day), 2, '0')
113117
case 'd':
114-
return String(this.mWeek)
118+
return String(this.$week)
115119
case 'dddd':
116-
return weeks[this.mWeek]
120+
return weeks[this.$week]
117121
case 'H':
118-
return String(this.mHour)
122+
return String(this.$hour)
119123
case 'HH':
120-
return padStart(String(this.mHour), 2, '0')
124+
return padStart(String(this.$hour), 2, '0')
121125
case 'm':
122-
return String(this.mMinute)
126+
return String(this.$minute)
123127
case 'mm':
124-
return padStart(String(this.mMinute), 2, '0')
128+
return padStart(String(this.$minute), 2, '0')
125129
case 's':
126-
return String(this.mSecond)
130+
return String(this.$second)
127131
case 'ss':
128-
return padStart(String(this.mSecond), 2, '0')
132+
return padStart(String(this.$second), 2, '0')
129133
case 'Z':
130134
return `${this.timeZoneString.slice(0, -2)}:00`
131135
case 'ZZ':

test/get-set.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ test('Month', () => {
99
expect(dayjs().month()).toBe(moment().month())
1010
})
1111

12+
test('Date', () => {
13+
expect(dayjs().date()).toBe(moment().date())
14+
})
15+

0 commit comments

Comments
 (0)