Skip to content

Commit 35f799e

Browse files
committed
fix(update to ms): update diff from second to millsecond
1 parent 290fcf7 commit 35f799e

3 files changed

Lines changed: 19 additions & 12 deletions

File tree

src/constant.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ export const SECONDS_A_DAY = SECONDS_A_HOUR * 24
44
export const SECONDS_A_WEEK = SECONDS_A_DAY * 7
55

66
export const MILLISECONDS_A_SECOND = 1000
7+
export const MILLISECONDS_A_WEEK = SECONDS_A_WEEK * MILLISECONDS_A_SECOND
78
export const MILLISECONDS_A_DAY = SECONDS_A_DAY * MILLISECONDS_A_SECOND
9+
export const MILLISECONDS_A_HOUR = SECONDS_A_HOUR * MILLISECONDS_A_SECOND
10+
export const MILLISECONDS_A_MINUTE = SECONDS_A_MINUTE * MILLISECONDS_A_SECOND

src/index.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,25 +127,25 @@ class Dayjs {
127127
switch (string) {
128128
case 'm':
129129
case 'minutes':
130-
step = Constant.SECONDS_A_MINUTE
130+
step = Constant.MILLISECONDS_A_MINUTE
131131
break
132132
case 'h':
133133
case 'hours':
134-
step = Constant.SECONDS_A_HOUR
134+
step = Constant.MILLISECONDS_A_HOUR
135135
break
136136
case 'd':
137137
case 'days':
138-
step = Constant.SECONDS_A_DAY
138+
step = Constant.MILLISECONDS_A_DAY
139139
break
140140
case 'w':
141141
case 'weeks':
142-
step = Constant.SECONDS_A_WEEK
142+
step = Constant.MILLISECONDS_A_WEEK
143143
break
144144
default: // s seconds
145-
step = 1
145+
step = Constant.MILLISECONDS_A_SECOND
146146
}
147-
const nextTimeStamp = this.unix() + (number * step)
148-
return new Dayjs(nextTimeStamp * 1000)
147+
const nextTimeStamp = this.valueOf() + (number * step)
148+
return new Dayjs(nextTimeStamp)
149149
}
150150

151151
subtract(number, string) {
@@ -195,10 +195,11 @@ class Dayjs {
195195

196196
diff(otherDate, unit, float = false) {
197197
const other = otherDate instanceof Dayjs ? otherDate : new Dayjs(otherDate)
198-
const returnFloat = result => (float ? result : Math.ceil(result))
198+
const returnFloat = result => (result > 0 ? Math.floor(result) : Math.ceil(result))
199+
const isReturnFloat = result => (float ? result : returnFloat(result))
199200
switch (unit) {
200201
case 'days':
201-
return returnFloat((this.valueOf() - other.valueOf()) / Constant.MILLISECONDS_A_DAY)
202+
return isReturnFloat((this.valueOf() - other.valueOf()) / Constant.MILLISECONDS_A_DAY)
202203
default:
203204
return this.valueOf() - other.valueOf()
204205
}

test/display.test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,16 @@ describe('Difference', () => {
7979
})
8080

8181
it('diff -> in day', () => {
82-
const dateString = '2018-04-21'
8382
const dayjsA = dayjs()
84-
const dayjsB = new Date(dateString)
83+
const dayjsB = dayjs().add(100, 'days')
84+
const dayjsC = dayjs().subtract(100, 'days')
8585
const momentA = moment()
86-
const momentB = new Date(dateString)
86+
const momentB = moment().add(100, 'days')
87+
const momentC = moment().subtract(100, 'days')
8788
expect(dayjsA.diff(dayjsB, 'days')).toBe(momentA.diff(momentB, 'days'))
8889
expect(dayjsA.diff(dayjsB, 'days', true)).toBe(momentA.diff(momentB, 'days', true))
90+
expect(dayjsA.diff(dayjsC, 'days')).toBe(momentA.diff(momentC, 'days'))
91+
expect(dayjsA.diff(dayjsC, 'days', true)).toBe(momentA.diff(momentC, 'days', true))
8992
})
9093
})
9194

0 commit comments

Comments
 (0)