From 01a568b2fcc05fcad989bdbc7416708a25eef6ee Mon Sep 17 00:00:00 2001 From: huihuimoe <0w0@loli.pet> Date: Sun, 29 Apr 2018 16:17:23 +0800 Subject: [PATCH] Query method supports Dayjs constructor args --- ReadMe.md | 9 ++++++--- ReadMe.zh-CN.md | 9 ++++++--- src/index.js | 6 +++--- test/query.test.js | 3 +++ 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/ReadMe.md b/ReadMe.md index 838241c2a..7a7625c8e 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -455,33 +455,36 @@ dayjs().toString(); * returns a Boolean -Check if a `Dayjs` object is before another `Dayjs` object. +Check if a `Dayjs` is before another `Dayjs`. ```js dayjs().isBefore(Dayjs); dayjs().isBefore(dayjs()); // false +dayjs().isBefore(Date.now()); // false ``` #### Is Same * returns a Boolean -Check if a `Dayjs` object is same as another `Dayjs` object. +Check if a `Dayjs` is same as another `Dayjs`. ```js dayjs().isSame(Dayjs); dayjs().isSame(dayjs()); // true +dayjs().isSame(Date.now()); // true ``` #### Is After * returns a Boolean -Check if a `Dayjs` object is after another `Dayjs` object. +Check if a `Dayjs` is after another `Dayjs`. ```js dayjs().isAfter(Dayjs); dayjs().isAfter(dayjs()); // false +dayjs().isAfter(Date.now()); // false ``` #### Is Leap Year diff --git a/ReadMe.zh-CN.md b/ReadMe.zh-CN.md index 11b8b6c53..30c87fb14 100644 --- a/ReadMe.zh-CN.md +++ b/ReadMe.zh-CN.md @@ -346,26 +346,29 @@ dayjs().toString(); #### 是否之前 - return Boolean -检查一个 `Dayjs` 对象是否在另一个 `Dayjs` 对象时间之前。 +检查一个 `Dayjs` 是否在另一个 `Dayjs` 时间之前。 ```js dayjs().isBefore(Dayjs); dayjs().isBefore(dayjs()); // false +dayjs().isBefore(Date.now()); // false ``` #### 是否相同 - return Boolean -检查一个 `Dayjs` 对象是否和另一个 `Dayjs` 对象时间相同。 +检查一个 `Dayjs` 是否和另一个 `Dayjs` 时间相同。 ```js dayjs().isSame(Dayjs); dayjs().isSame(dayjs()); // true +dayjs().isSame(Date.now()); // true ``` #### 是否之后 - return Boolean -检查一个 `Dayjs` 对象是否在另一个 `Dayjs` 对象时间之后。 +检查一个 `Dayjs` 是否在另一个 `Dayjs` 时间之后。 ```js dayjs().isAfter(Dayjs); dayjs().isAfter(dayjs()); // false +dayjs().isAfter(Date.now()); // false ``` #### 是否闰年 - return Boolean diff --git a/src/index.js b/src/index.js index 7affa875d..d9a5e9a95 100644 --- a/src/index.js +++ b/src/index.js @@ -42,15 +42,15 @@ class Dayjs { } isSame(that) { - return this.valueOf() === that.valueOf() + return this.valueOf() === (that instanceof Dayjs ? that : new Dayjs(that)).valueOf() } isBefore(that) { - return this.valueOf() < that.valueOf() + return this.valueOf() < (that instanceof Dayjs ? that : new Dayjs(that)).valueOf() } isAfter(that) { - return this.valueOf() > that.valueOf() + return this.valueOf() > (that instanceof Dayjs ? that : new Dayjs(that)).valueOf() } year() { diff --git a/test/query.test.js b/test/query.test.js index cf864dcc2..132d1828c 100644 --- a/test/query.test.js +++ b/test/query.test.js @@ -23,7 +23,10 @@ it('Is Before Is After Is Same', () => { const dayB = dayA.clone().add(1, 'day') const dayC = dayA.clone().subtract(1, 'day') expect(dayC.isBefore(dayA)).toBe(true) + expect(dayC.isBefore(Date.now() + 1)).toBe(true) expect(dayA.isSame(instance())).toBe(true) + expect(dayA.isSame(Date.now())).toBe(true) expect(dayB.isAfter(dayA)).toBe(true) + expect(dayB.isAfter(Date.now() - 1)).toBe(true) }) })