-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into DAP-1006-email-template
- Loading branch information
Showing
7 changed files
with
96 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,34 @@ | ||
export const formatDate = (date?: string | null): string => { | ||
if (!date || date === "") return ""; | ||
|
||
// Check if the string is in ISO format | ||
const isISO = date.includes("T"); | ||
if (!isISO) { | ||
// Handle non-ISO date string in the format YYYY/MM/DD | ||
const parts = date.split("/"); | ||
if (parts.length !== 3) { | ||
return ""; // Return an empty string if the format is unexpected | ||
} | ||
|
||
const [year, month, day] = parts; | ||
if (!year || !month || !day) { | ||
return ""; // Return an empty string if any part is missing | ||
} | ||
|
||
const parsedYear = year.trim(); | ||
const parsedMonth = month.trim().padStart(2, "0"); | ||
const parsedDay = day.trim().padStart(2, "0"); | ||
return `${parsedYear}-${parsedMonth}-${parsedDay}`; | ||
} | ||
|
||
// Handle ISO date strings (UTC timezone) | ||
const parsedDate = new Date(date); | ||
if (Number.isNaN(parsedDate.getTime())) { | ||
return date; // Return the original value if it's not a valid date | ||
} | ||
|
||
const year = parsedDate.getUTCFullYear(); | ||
const month = String(parsedDate.getUTCMonth() + 1).padStart(2, "0"); | ||
const day = String(parsedDate.getUTCDate()).padStart(2, "0"); | ||
const year = parsedDate.getUTCFullYear(); | ||
return `${month}-${day}-${year}`; | ||
return `${year}-${month}-${day}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { formatDate } from "@/utils"; | ||
|
||
describe("formatDate", () => { | ||
it("should return an empty string if date is undefined", () => { | ||
expect(formatDate(undefined)).toBe(""); | ||
}); | ||
|
||
it("should return an empty string if date is null", () => { | ||
expect(formatDate(null)).toBe(""); | ||
}); | ||
|
||
it("should return an empty string if date is an empty string", () => { | ||
expect(formatDate("")).toBe(""); | ||
}); | ||
|
||
it("should format a valid ISO date string to YYYY-MM-DD", () => { | ||
expect(formatDate("2024-11-25T00:00:00Z")).toBe("2024-11-25"); | ||
}); | ||
|
||
it("should format a valid date string in YYYY/MM/DD to YYYY-MM-DD", () => { | ||
expect(formatDate("2024/11/25")).toBe("2024-11-25"); | ||
}); | ||
|
||
it("should return an empty string for unexpected non-ISO formats", () => { | ||
expect(formatDate("Invalid Date Format")).toBe(""); | ||
}); | ||
|
||
it("should handle leap year dates correctly", () => { | ||
expect(formatDate("2024-02-29T12:00:00Z")).toBe("2024-02-29"); | ||
}); | ||
|
||
it("should correctly format non-UTC ISO times", () => { | ||
expect(formatDate("2024-11-25T23:59:59-08:00")).toBe("2024-11-26"); | ||
}); | ||
|
||
it("should handle YYYY/MM/DD format with extra spaces", () => { | ||
expect(formatDate(" 2024 / 11 / 25 ")).toBe("2024-11-25"); | ||
}); | ||
|
||
it("should return an empty string if the YYYY/MM/DD format is incomplete", () => { | ||
expect(formatDate("2024/11")).toBe(""); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters