Skip to content

feat(formatDate): adds new date helper function #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions cypress/test/functions/dates/format-date.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* Please refer to the terms of the license agreement in the root of the project
*
* (c) 2025 Feedzai
*/
import { formatDate } from "../../../../src/functions";

const DEFAULT_LOCALES = { locales: "en-US" };

describe("formatDate", () => {
it("should format date string correctly", () => {
expect(formatDate({ date: "2024-01-15", ...DEFAULT_LOCALES })).to.equal(
"January 15"
);
expect(formatDate({ date: "2024-12-31", ...DEFAULT_LOCALES })).to.equal(
"December 31"
);
});

it("should format Date object correctly", () => {
const dateObject = new Date("2024-01-15");

expect(formatDate({ date: dateObject, ...DEFAULT_LOCALES })).to.equal(
"January 15"
);
});

it("should format timestamp correctly", () => {
const timestamp = new Date("2024-01-15").getTime();

expect(formatDate({ date: timestamp, ...DEFAULT_LOCALES })).to.equal(
"January 15"
);
});

it("should handle different date string formats", () => {
expect(formatDate({ date: "2024/01/15", ...DEFAULT_LOCALES })).to.equal(
"January 15"
);
expect(formatDate({ date: "01-15-2024", ...DEFAULT_LOCALES })).to.equal(
"January 15"
);
expect(formatDate({ date: "15 Jan 2024", ...DEFAULT_LOCALES })).to.equal(
"January 15"
);
});

it("should include year when specified", () => {
const customOptions = { year: "numeric" } as const;

expect(
formatDate({
date: "2024-01-15",
options: customOptions,
...DEFAULT_LOCALES,
})
).to.equal("January 15, 2024");
});

it("should show short month format", () => {
const customOptions = { month: "short" } as const;

expect(
formatDate({
date: "2024-01-15",
options: customOptions,
...DEFAULT_LOCALES,
})
).to.equal("Jan 15");
});

it("should include weekday", () => {
const customOptions = { weekday: "long" } as const;

expect(
formatDate({
date: "2024-01-15",
options: customOptions,
...DEFAULT_LOCALES,
})
).to.equal("Monday, January 15");
});

it("should respect all provided options while maintaining defaults", () => {
const customOptions = {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
} as const;

expect(
formatDate({
date: "2024-01-15",
options: customOptions,
...DEFAULT_LOCALES,
})
).to.equal("Monday, January 15, 2024");
});

it("should handle invalid dates", () => {
expect(() => formatDate({ date: "invalid-date" })).throw();
expect(() => formatDate({ date: "2024-13-45" })).throw();
});

it("should handle empty input", () => {
expect(() => formatDate({ date: "" })).throw();
});

it("should handle null and undefined", () => {
expect(() => formatDate({ date: null })).throw();
expect(() => formatDate()).throw();
});
});
27 changes: 27 additions & 0 deletions docs/docs/functions/dates/format-date.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
title: formatDate
---

Formats a date value into a localized string representation using the `toLocaleDateString()` method.

### API

```typescript
interface IFormatDateConfig {
date: ConstructorParameters<typeof Date>[0];
locales?: Intl.LocalesArgument;
options?: Intl.DateTimeFormatOptions;
}

function formatDate(config: IFormatDateConfig): string;
```

### Usage

```typescript
import { formatDate } from "@feedzai/js-utilities";

const formattedDate = formatDate({ date: "2023-12-25" });

console.log(formattedDate); // "December 25"
```
43 changes: 43 additions & 0 deletions src/functions/dates/format-date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Please refer to the terms of the license agreement in the root of the project
*
* (c) 2025 Feedzai
*/

interface IFormatDateConfig {
date: ConstructorParameters<typeof Date>[0];
locales?: Intl.LocalesArgument;
options?: Intl.DateTimeFormatOptions;
}

const DEFAULT_OPTIONS = {
month: "long",
day: "numeric",
} as const;

/**
* Formats a date value into a localized string representation.
*
* @param {IFormatDateConfig} config - Configuration object for date formatting.
* @returns {string} A localized date string. By default, returns the date in "{Month} {Day}" format (e.g., "December 25")
*
* @example
* formatDate({ date: "2023-12-25" })
* // => "December 25"
*/
export const formatDate = ({
date: dateValue,
locales,
options,
}: IFormatDateConfig) => {
const date = new Date(dateValue);

if (!dateValue || isNaN(date.getTime())) {
throw new Error("Invalid date value");
}

return date.toLocaleDateString(locales, {
...DEFAULT_OPTIONS,
...options,
});
};
1 change: 1 addition & 0 deletions src/functions/dates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
* (c) 2024 Feedzai
*/
export * from "./get-browser-timezone";
export * from "./format-date";