Skip to content

Commit

Permalink
feat(diff valueOf): diff valueOf
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed Apr 13, 2018
1 parent 85a3b76 commit 8d127dc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ class Dayjs {
}

unix() {
return Math.floor(this.valueOf() / 1000)
}

valueOf() {
// timezone(hour) * 60 * 60 * 1000 => ms
const zonePad = !this.utc ? 0 : this.timeZone * 60 * 60 * 1000
return Math.floor((this.$date.getTime() + zonePad) / 1000)
return this.$date.getTime() + zonePad
}

toString() {
Expand Down Expand Up @@ -139,6 +143,11 @@ class Dayjs {
}
})
}

diff(otherDate) {
const other = otherDate instanceof Dayjs ? otherDate : new Dayjs(otherDate)
return this.valueOf() - other.valueOf()
}
}

export default config => (
Expand Down
19 changes: 19 additions & 0 deletions test/display.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,22 @@ test('Format Complex with other string - : / ', () => {
expect(dayjs().format(string)).toBe(moment().format(string))
})

it('Difference', () => {
const dateString = '20110101'

const dayjsA = dayjs()
const dayjsB = dayjs(dateString)

const momentA = moment()
const momentB = moment(dateString)
expect(dayjsA.diff(dayjsB)).toBe(momentA.diff(momentB))
})

it('Unix Timestamp (milliseconds) ', () => {
expect(dayjs().valueOf()).toBe(moment().valueOf())
})

it('Unix Timestamp (seconds) ', () => {
expect(dayjs().unix()).toBe(moment().unix())
})

0 comments on commit 8d127dc

Please sign in to comment.