Skip to content

Commit

Permalink
Add more date utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
drebrez committed Jan 13, 2025
1 parent 072acf3 commit 7a2bfd1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- `getStartOfDay`, `getEndOfDay` and `getLastDayOfYear` date utility functions

## [1.1.0] - 2024-10-17

- export `boolean` utilities
Expand Down
40 changes: 39 additions & 1 deletion src/lib/date.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import {
dateIsWeekend,
dateDifferenceInDays,
getNextBusinessDate,
getStartOfDay,
getEndOfDay,
getLastDayOfYear,
} from "./date";

describe("date tests", () => {
Expand Down Expand Up @@ -106,6 +109,41 @@ describe("date tests", () => {
[42 as unknown as Date, new Date(Number.NaN)],
["test" as unknown as Date, new Date(Number.NaN)],
])("getNextBusinessDate", (date, expected) => {
expect(getNextBusinessDate(date).getDate()).toBe(expected.getDate());
expect(getNextBusinessDate(date).getTime()).toBe(expected.getTime());
});

test.each([
[new Date(2024, 0, 1, 12, 45, 25, 333), new Date(2024, 0, 1, 0, 0, 0, 0)],
[null as unknown as Date, new Date(Number.NaN)],
[undefined as unknown as Date, new Date(Number.NaN)],
[42 as unknown as Date, new Date(Number.NaN)],
["test" as unknown as Date, new Date(Number.NaN)],
])("getStartOfDay", (date, expected) => {
expect(getStartOfDay(date).getTime()).toBe(expected.getTime());
});

test.each([
[new Date(2024, 0, 1, 12, 45, 25, 333), new Date(2024, 0, 1, 23, 59, 59, 999)],
[null as unknown as Date, new Date(Number.NaN)],
[undefined as unknown as Date, new Date(Number.NaN)],
[42 as unknown as Date, new Date(Number.NaN)],
["test" as unknown as Date, new Date(Number.NaN)],
])("getEndOfDay", (date, expected) => {
expect(getEndOfDay(date).getTime()).toBe(expected.getTime());
});

test.each([
[null as unknown as Date, new Date(Number.NaN)],
[undefined as unknown as Date, new Date(Number.NaN)],
[42 as unknown as Date, new Date(Number.NaN)],
["test" as unknown as Date, new Date(Number.NaN)],
])("getLastDayOfYear", (date, expected) => {
expect(getLastDayOfYear(date).getTime()).toBe(expected.getTime());
});

test("getLastDayOfYear all days", () => {
for (let date = new Date(2024, 0, 1); date < new Date(2025, 0, 1); date.setDate(date.getDate() + 1)) {
expect(getLastDayOfYear(date).getTime()).toBe(new Date(2024, 11, 31, 0, 0, 0, 0).getTime());
}
});
});
30 changes: 30 additions & 0 deletions src/lib/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {
isWeekend,
differenceInCalendarDays,
addBusinessDays,
startOfDay,
endOfDay,
lastDayOfYear,
} from "date-fns";

/**
Expand Down Expand Up @@ -73,3 +76,30 @@ export function dateDifferenceInDays(from: Date, to: Date): number {
export function getNextBusinessDate(date: Date): Date {
return dateIsValid(date) ? addBusinessDays(date, 1) : new Date(Number.NaN);
}

/**
* Get the start of a day for the given date
* @param date The date
* @returns The start of a day date
*/
export function getStartOfDay(date: Date): Date {
return dateIsValid(date) ? startOfDay(date) : new Date(Number.NaN);
}

/**
* Get the end of a day for the given date
* @param date The date
* @returns The end of a day date
*/
export function getEndOfDay(date: Date): Date {
return dateIsValid(date) ? endOfDay(date) : new Date(Number.NaN);
}

/**
* Get the last day of a year for the given date
* @param date The date
* @returns The last day of the date year
*/
export function getLastDayOfYear(date: Date): Date {
return dateIsValid(date) ? lastDayOfYear(date) : new Date(Number.NaN);
}

0 comments on commit 7a2bfd1

Please sign in to comment.