From ff1157a4a44a6770ca843c4db24931d740b19968 Mon Sep 17 00:00:00 2001 From: Karen Shaw Date: Thu, 12 Dec 2024 21:47:30 +0000 Subject: [PATCH] Show leading zeros on minutes in shared link expiration display --- lib/utils/date-helpers.test.ts | 30 ++++++++++++++++++++++++++++++ lib/utils/date-helpers.ts | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 lib/utils/date-helpers.test.ts diff --git a/lib/utils/date-helpers.test.ts b/lib/utils/date-helpers.test.ts new file mode 100644 index 00000000..f8afc6ab --- /dev/null +++ b/lib/utils/date-helpers.test.ts @@ -0,0 +1,30 @@ +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", () => { + test("should format date correctly in long format", () => { + const result = formatDateLong("2023-11-14T13:14:00Z"); + expect(result).toBe("November 14, 2023, 1:14pm"); + }); + + test("should return empty string if date is undefined", () => { + const result = formatDateLong(undefined); + expect(result).toBe(""); + }); + + test("should format time correctly with leading zeroes in minutes", () => { + const result = formatDateLong("2023-11-14T01:04:00Z"); + expect(result).toBe("November 14, 2023, 1:04am"); + }); +}); 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" }`; }