diff --git a/lib/utils/date-helpers.test.ts b/lib/utils/date-helpers.test.ts new file mode 100644 index 00000000..56e2c02d --- /dev/null +++ b/lib/utils/date-helpers.test.ts @@ -0,0 +1,72 @@ +import { formatDate, formatDateLong } from "./date-helpers"; + +describe("formatDate", () => { + test("should format date correctly", () => { + const result = formatDate("2023-11-14T13:14:00Z"); + expect(result).toBe("2023-10-2"); // Note: getMonth() is zero-based, getDay() returns day of the week + }); + + test("should handle invalid date", () => { + const result = formatDate("invalid-date"); + expect(result).toBe("NaN-NaN-NaN"); + }); +}); + +describe("formatDateLong", () => { + it("should return a properly formatted date string for a valid date input", () => { + const inputDate = "2023-11-14T13:14:00Z"; // UTC time + const dateObj = new Date(inputDate); + + // Dynamically generate the expected output based on the local time zone + const expectedOutput = `${dateObj.toLocaleString("en-US", { month: "long" })} ${dateObj.getDate()}, ${dateObj.getFullYear()}, ${ + dateObj.getHours() > 12 ? dateObj.getHours() - 12 : dateObj.getHours() + }:${dateObj.getMinutes().toString().padStart(2, "0")}${ + dateObj.getHours() >= 12 ? "pm" : "am" + }`; + + expect(formatDateLong(inputDate)).toBe(expectedOutput); + }); + + it("should return an empty string for an undefined input", () => { + expect(formatDateLong(undefined)).toBe(""); + }); + + it("should handle midnight correctly", () => { + const inputDate = "2023-11-14T00:00:00Z"; // UTC time + const dateObj = new Date(inputDate); + + const expectedOutput = `${dateObj.toLocaleString("en-US", { month: "long" })} ${dateObj.getDate()}, ${dateObj.getFullYear()}, ${ + dateObj.getHours() > 12 ? dateObj.getHours() - 12 : dateObj.getHours() + }:${dateObj.getMinutes().toString().padStart(2, "0")}${ + dateObj.getHours() >= 12 ? "pm" : "am" + }`; + + expect(formatDateLong(inputDate)).toBe(expectedOutput); + }); + + it("should handle noon correctly", () => { + const inputDate = "2023-11-14T12:00:00Z"; // UTC time + const dateObj = new Date(inputDate); + + const expectedOutput = `${dateObj.toLocaleString("en-US", { month: "long" })} ${dateObj.getDate()}, ${dateObj.getFullYear()}, ${ + dateObj.getHours() > 12 ? dateObj.getHours() - 12 : dateObj.getHours() + }:${dateObj.getMinutes().toString().padStart(2, "0")}${ + dateObj.getHours() >= 12 ? "pm" : "am" + }`; + + expect(formatDateLong(inputDate)).toBe(expectedOutput); + }); + + it("should pad minutes correctly", () => { + const inputDate = "2023-11-14T13:04:00Z"; // UTC time + const dateObj = new Date(inputDate); + + const expectedOutput = `${dateObj.toLocaleString("en-US", { month: "long" })} ${dateObj.getDate()}, ${dateObj.getFullYear()}, ${ + dateObj.getHours() > 12 ? dateObj.getHours() - 12 : dateObj.getHours() + }:${dateObj.getMinutes().toString().padStart(2, "0")}${ + dateObj.getHours() >= 12 ? "pm" : "am" + }`; + + expect(formatDateLong(inputDate)).toBe(expectedOutput); + }); +}); diff --git a/lib/utils/date-helpers.ts b/lib/utils/date-helpers.ts index 4e26bad8..c664b1b4 100644 --- a/lib/utils/date-helpers.ts +++ b/lib/utils/date-helpers.ts @@ -12,7 +12,7 @@ export function formatDateLong(date: string | undefined): string { // Write a JS Date object to a string in the format like "November 14, 2023, 1:14pm" return `${dateObj.toLocaleString("en-US", { month: "long", - })} ${dateObj.getDate()}, ${dateObj.getFullYear()}, ${hour}:${dateObj.getMinutes()}${ + })} ${dateObj.getDate()}, ${dateObj.getFullYear()}, ${hour}:${dateObj.getMinutes().toString().padStart(2, "0")}${ dateObj.getHours() > 12 ? "pm" : "am" }`; }