From 8d127dcf35e030df17dd6da1ea332e9359c00bdf Mon Sep 17 00:00:00 2001 From: iamkun Date: Fri, 13 Apr 2018 14:21:40 +0800 Subject: [PATCH] feat(diff valueOf): diff valueOf --- src/index.js | 11 ++++++++++- test/display.test.js | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 2ce109b2c..c695ebf59 100644 --- a/src/index.js +++ b/src/index.js @@ -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() { @@ -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 => ( diff --git a/test/display.test.js b/test/display.test.js index 6fba2a643..1802e1d30 100644 --- a/test/display.test.js +++ b/test/display.test.js @@ -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()) +}) +