Skip to content

Commit 5b4415a

Browse files
committed
fix(isdatebefore): update date comparison logic
1 parent d8b9d5b commit 5b4415a

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

src/isDateBefore/isDateBefore.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ describe("Util: isDateBefore", () => {
2828
},
2929
{
3030
statement: "Should return false if date1 is not before date2",
31-
date1: new Date("2020-12-28"),
32-
date2: new Date("2020-12-25"),
31+
date1: new Date("2023-03-04"),
32+
date2: new Date("2023-02-15"),
3333
result: false,
3434
},
3535
{

src/isDateBefore/isDateBefore.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,9 @@ import { isValidDate } from "../isValidDate";
77
* @returns {boolean} True if date `a` comes before date `b`
88
*/
99
export function isDateBefore(a: Date, b: Date): boolean {
10-
if (!isValidDate(a) || !isValidDate(b)) {
11-
return a < b;
12-
} else {
13-
return (
14-
a.getFullYear() < b.getFullYear() ||
15-
a.getMonth() < b.getMonth() ||
16-
a.getDate() < b.getDate()
17-
);
10+
if (isValidDate(a) && isValidDate(b)) {
11+
return a.setHours(0, 0, 0).valueOf() < b.setHours(0, 0, 0).valueOf();
1812
}
13+
14+
return a < b;
1915
}

0 commit comments

Comments
 (0)