Skip to content
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

Add helper functions for localized date strings #749

Merged
merged 4 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Define the supported locales

export const getDateFormatForLocaleCode = (locale: string): string => {
const formatter = new Intl.DateTimeFormat(locale, { dateStyle: "short" });
const parts = formatter.formatToParts(new Date());
const formatMap: { [key: string]: string } = {
year: "yyyy",
month: "MM",
day: "dd",
};
return parts.map((part) => formatMap[part.type] || part.value).join("");
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Locale } from "date-fns";
import { da, de, enGB, enUS, es, fr, nb, nl, pl, sv, deAT } from "date-fns/locale";

type LocalesMap = {
[key: string]: Locale;
};

const locales: LocalesMap = {
"en-US": enUS,
"en-GB": enGB,
"de-AT": deAT,
"de-DE": de,
fr,
de,
es,
sv,
pl,
da,
nl,
nb,
};

export const getLocaleForLocaleCode = (
localeCode: string
): Locale | undefined => {
return locales[localeCode];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { format } from "date-fns";
import { getDateFormatForLocaleCode } from "./DateFormatProvider";
import { getLocaleForLocaleCode } from "./LocaleMapper";

export const formatLocalizedDate = (date: Date, localeCode: string): string =>
format(date, getDateFormatForLocaleCode(localeCode), {
locale: getLocaleForLocaleCode(localeCode),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { parse } from "date-fns";
import { getDateFormatForLocaleCode } from "./DateFormatProvider";
import { getLocaleForLocaleCode } from "./LocaleMapper";

export const parseLocalizedDateString = (
dateString: string,
localeCode: string,
referenceDate: Date = new Date()
): Date =>
parse(dateString, getDateFormatForLocaleCode(localeCode), referenceDate, {
locale: getLocaleForLocaleCode(localeCode),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { getDateFormatForLocaleCode } from "../DateFormatProvider";

describe("DateFormatProvider", () => {
describe("getDateFormatForLocaleCode", () => {
describe("for Sweden", () => {
it("returns correct date format", () => {
expect(getDateFormatForLocaleCode("sv")).toBe("yyyy-MM-dd");
});
});
describe("for UK", () => {
it("returns correct date format", () => {
expect(getDateFormatForLocaleCode("en-GB")).toBe("dd/MM/yyyy");
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { formatLocalizedDate } from "../LocalizedDateFormatter";

describe("LocalizedDateFormatter", () => {
describe("formatLocalizedDate", () => {
describe("for Sweden", () => {
it("works", () => {
expect(formatLocalizedDate(new Date(2024, 3, 14), "sv")).toBe(
"2024-04-14"
);
});
});
describe("for UK", () => {
it("works", () => {
expect(formatLocalizedDate(new Date(2024, 3, 14), "en-GB")).toBe(
"14/04/2024"
);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { parseLocalizedDateString } from "../LocalizedDateParser";

describe("LocalizedDateParser", () => {
describe("parseLocalizedDateStringElseUndefined", () => {
describe("for invalid localeCode", () => {
it("returns invalid date", () => {
const d = parseLocalizedDateString("2024-03-12", "hejhej");
expect(isNaN(d.getTime())).toBe(true);
});
});
describe("for Sweden", () => {
it("works", () => {
const d = parseLocalizedDateString("2024-03-12", "sv");
expect(d?.getFullYear()).toBe(2024);
expect(d?.getMonth()).toBe(2);
expect(d?.getDate()).toBe(12);
});
});
describe("for UK", () => {
it("works", () => {
const d = parseLocalizedDateString("12/03/2024", "en-GB");
expect(d?.getFullYear()).toBe(2024);
expect(d?.getMonth()).toBe(2);
expect(d?.getDate()).toBe(12);
});
});
});
});
4 changes: 4 additions & 0 deletions packages/calendar/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ export * from "./features/month-picker/MonthPicker";
export * from "./features/year-picker/YearPicker";
export * from "./features/preset-picker/PresetPicker";
export * from "./components/input-types/date-range-dual-text-input/DateRangeDualTextInput";
export * from "./features/localize-date-format/LocalizedDateFormatter";
export * from "./features/localize-date-format/LocalizedDateParser";
export * from "./features/localize-date-format/DateFormatProvider";
export * from "./features/localize-date-format/LocaleMapper";
Loading