Skip to content

Commit

Permalink
fix: update daysDiff function
Browse files Browse the repository at this point in the history
  • Loading branch information
altaywtf committed Oct 25, 2023
1 parent 5434769 commit a1b2a2c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/date/date.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ describe('date', () => {
});

describe('daysDiff', () => {
beforeAll(() => {
jest.useFakeTimers().setSystemTime(new Date('2020-02-05'));
});

it('calculates the difference between two dates, in calendar days', () => {
expect(daysDiff('2023-06-03T18:30:03Z')).toBe(1);
expect(daysDiff('2020-02-05T16:13:28')).toBe(0);
expect(daysDiff('2020-02-04T06:51:43')).toBe(1);
});
});
});
6 changes: 3 additions & 3 deletions src/date/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export const daysDiff = (
date1: UTCTimestamp,
date2: UTCTimestamp = new Date().toISOString()
) => {
return Math.ceil(
Math.abs(dayjs(ensureUTC(date1)).diff(ensureUTC(date2), 'day', true))
);
const day1 = dayjs(ensureUTC(date1)).startOf('day');
const day2 = dayjs(ensureUTC(date2)).startOf('day');
return day2.diff(day1, 'day');
};

export const getUnixTimestamp = (date: UTCTimestamp) => {
Expand Down

0 comments on commit a1b2a2c

Please sign in to comment.