From 71e577eadaf0b3c0c056dc66765065f04b082540 Mon Sep 17 00:00:00 2001 From: "Ewe Seong, Yeoh" Date: Thu, 16 Feb 2023 17:21:11 +0800 Subject: [PATCH] fix: ignore time in date comparison utilities --- src/isDateAfter/isDateAfter.ts | 9 ++++++--- src/isDateBefore/isDateBefore.test.ts | 7 +++++++ src/isDateBefore/isDateBefore.ts | 7 +++++-- src/isSameDate/isSameDate.test.ts | 7 +++++++ src/isSameDate/isSameDate.ts | 15 +++++++++------ 5 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/isDateAfter/isDateAfter.ts b/src/isDateAfter/isDateAfter.ts index 1432a9e..12f08a0 100644 --- a/src/isDateAfter/isDateAfter.ts +++ b/src/isDateAfter/isDateAfter.ts @@ -1,14 +1,17 @@ import { isValidDate } from "../isValidDate"; /** - * Compare two dates and return true if the first is greater than the second ignoring the time + * Compare two dates and return true if the first date is greater than the second date ignoring the time. + * * @param {Date} a The first date * @param {Date} b The second date - * @returns {boolean} True if date `a` comes after than date `b` + * @returns {boolean} True if date `a` comes after date `b` */ export function isDateAfter(a: Date, b: Date): boolean { if (isValidDate(a) && isValidDate(b)) { - return a.setHours(0, 0, 0).valueOf() > b.setHours(0, 0, 0).valueOf(); + a.setHours(0, 0, 0, 0); + b.setHours(0, 0, 0, 0); + return a.valueOf() > b.valueOf(); } return a > b; diff --git a/src/isDateBefore/isDateBefore.test.ts b/src/isDateBefore/isDateBefore.test.ts index 8a10384..dd8024f 100644 --- a/src/isDateBefore/isDateBefore.test.ts +++ b/src/isDateBefore/isDateBefore.test.ts @@ -38,6 +38,13 @@ describe("Util: isDateBefore", () => { date2: new Date("2020-01-01"), result: false, }, + { + statement: + "Should ignore the time and return false if the two dates are equal", + date1: new Date("2019-11-11 23:00:00"), + date2: new Date("2019-11-11 11:00:00"), + result: false, + }, ]; testCases.map((testCase: TestCase) => { test(`- ${testCase.statement} | params: ${JSON.stringify({ diff --git a/src/isDateBefore/isDateBefore.ts b/src/isDateBefore/isDateBefore.ts index 9594cbf..e649b7c 100644 --- a/src/isDateBefore/isDateBefore.ts +++ b/src/isDateBefore/isDateBefore.ts @@ -1,14 +1,17 @@ import { isValidDate } from "../isValidDate"; /** - * Compare two dates and return true if the first is less (or before) than the second ignoring the time + * Compare two dates and return true if the first date is lesser than the second date ignoring the time. + * * @param {Date} a The first date * @param {Date} b The second date * @returns {boolean} True if date `a` comes before date `b` */ export function isDateBefore(a: Date, b: Date): boolean { if (isValidDate(a) && isValidDate(b)) { - return a.setHours(0, 0, 0).valueOf() < b.setHours(0, 0, 0).valueOf(); + a.setHours(0, 0, 0, 0); + b.setHours(0, 0, 0, 0); + return a.valueOf() < b.valueOf(); } return a < b; diff --git a/src/isSameDate/isSameDate.test.ts b/src/isSameDate/isSameDate.test.ts index 9989eae..638f51e 100644 --- a/src/isSameDate/isSameDate.test.ts +++ b/src/isSameDate/isSameDate.test.ts @@ -38,6 +38,13 @@ describe("Util: isSameDate", () => { date2: new Date("2019-12-12"), result: false, }, + { + statement: + "Should ignore the time and return true if the two dates are equal", + date1: new Date("2019-11-11 23:00:00"), + date2: new Date("2019-11-11 11:00:00"), + result: true, + }, ]; testCases.map((testCase: TestCase) => { test(`- ${testCase.statement} | params: ${JSON.stringify({ diff --git a/src/isSameDate/isSameDate.ts b/src/isSameDate/isSameDate.ts index e459d8a..23be3ef 100644 --- a/src/isSameDate/isSameDate.ts +++ b/src/isSameDate/isSameDate.ts @@ -1,15 +1,18 @@ import { isValidDate } from "../isValidDate"; /** - * Compare two dates and return true they are the same date ignoring the time + * Compare two dates and return true if the first date is the same as the second date ignoring the time. + * * @param {Date} a The first date, * @param {Date} b The second date - * @returns {boolean} True if date are the same + * @returns {boolean} True if date `a` is the same as date `b` */ export function isSameDate(a: Date, b: Date): boolean { - if (!isValidDate(a) || !isValidDate(b)) { - return false; - } else { - return a.toLocaleDateString() === b.toLocaleDateString(); + if (isValidDate(a) && isValidDate(b)) { + a.setHours(0, 0, 0, 0); + b.setHours(0, 0, 0, 0); + return a.valueOf() === b.valueOf(); } + + return a > b; }