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 43482c8
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
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);
});
});
2 changes: 1 addition & 1 deletion lib/utils/date-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}`;
}

0 comments on commit 43482c8

Please sign in to comment.