Skip to content

Commit

Permalink
Show leading zeros on minutes in shared link expiration display
Browse files Browse the repository at this point in the history
  • Loading branch information
kdid committed Dec 13, 2024
1 parent 59350a9 commit 83ec6cc
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 8 deletions.
72 changes: 72 additions & 0 deletions lib/utils/date-helpers.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
13 changes: 5 additions & 8 deletions lib/utils/date-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ export function formatDateLong(date: string | undefined): string {
if (!date) return "";

const dateObj = new Date(date);
const hour =
dateObj.getHours() > 12 ? dateObj.getHours() - 12 : dateObj.getHours();
// 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.getHours() > 12 ? "pm" : "am"
}`;
const hour = dateObj.getHours() % 12 || 12; // Handle midnight and noon
const minutes = dateObj.getMinutes().toString().padStart(2, "0");
const ampm = dateObj.getHours() >= 12 ? "pm" : "am";

return `${dateObj.toLocaleString("en-US", { month: "long" })} ${dateObj.getDate()}, ${dateObj.getFullYear()}, ${hour}:${minutes}${ampm}`;
}

0 comments on commit 83ec6cc

Please sign in to comment.