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

Deploy to production #397

Merged
merged 2 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
73 changes: 73 additions & 0 deletions lib/utils/date-helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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 in the local time zone", () => {
const inputDate = "2023-11-14T00:00:00Z"; // Midnight in UTC
const dateObj = new Date(inputDate);

const hour = dateObj.getHours() % 12 || 12;
const minutes = dateObj.getMinutes().toString().padStart(2, "0");
const ampm = dateObj.getHours() >= 12 ? "pm" : "am";

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

expect(formatDateLong(inputDate)).toBe(expectedOutput);
});

it("should handle noon correctly in the local time zone", () => {
const inputDate = "2023-11-14T12:00:00Z"; // Noon in UTC
const dateObj = new Date(inputDate);

// Dynamically calculate expected output in the local time zone
const hour = dateObj.getHours() % 12 || 12;
const minutes = dateObj.getMinutes().toString().padStart(2, "0");
const ampm = dateObj.getHours() >= 12 ? "pm" : "am";

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

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}`;
}
Loading