Skip to content

Commit

Permalink
Merge pull request #253 from sebgroup/develop
Browse files Browse the repository at this point in the history
next release
  • Loading branch information
eweseong authored Feb 16, 2023
2 parents 332915b + c444b03 commit ef37827
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
9 changes: 6 additions & 3 deletions src/isDateAfter/isDateAfter.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
7 changes: 7 additions & 0 deletions src/isDateBefore/isDateBefore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
7 changes: 5 additions & 2 deletions src/isDateBefore/isDateBefore.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
7 changes: 7 additions & 0 deletions src/isSameDate/isSameDate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
15 changes: 9 additions & 6 deletions src/isSameDate/isSameDate.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit ef37827

Please sign in to comment.