-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86e2226
commit 932f803
Showing
2 changed files
with
26 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,32 @@ | ||
import { format } from "date-fns"; | ||
import { format, parseISO } from "date-fns"; | ||
/** | ||
* Formats a given date into a specified display format. | ||
* | ||
* The function converts the input date into a string representation according to | ||
* the specified format. By default, it uses the "d.MM.yyyy" format, but a different | ||
* format can be provided if needed. | ||
* The function converts the input date string into a date object and then formats it | ||
* according to the "do LLL yyyy" format, which will display the date in the format | ||
* "23rd Aug 2024". | ||
* | ||
* @param date - The date to format. This can be a string or a `Date` object. | ||
* @param outputFormat - The format string that specifies the desired output format. | ||
* Defaults to "d.MM.yyyy" if not specified. | ||
* @returns A string representing the formatted date based on the `outputFormat`. | ||
* @param dateString - The date string to format. This should be in the ISO 8601 format | ||
* (e.g., "2024-08-23T04:08:06.000Z"). | ||
* @returns A string representing the formatted date. | ||
* | ||
* @example | ||
* formatDisplayDate("2024-08-05"); | ||
* // Returns "5.08.2024" (using default format) | ||
* formatDisplayDate("2024-08-23T04:08:06.000Z"); | ||
* // Returns "23rd Aug 2024" | ||
* | ||
* formatDisplayDate(new Date(), "yyyy/MM/dd"); | ||
* // Returns "2024/08/05" (current date in specified format) | ||
* | ||
* formatDisplayDate("2024-08-05", "MMMM d, yyyy"); | ||
* // Returns "August 5, 2024" | ||
* formatDisplayDate("invalid date"); | ||
* // Returns "Invalid date" | ||
*/ | ||
export const formatDisplayDate = ( | ||
date: string | Date, | ||
outputFormat = "d.MM.yyyy" | ||
) => format(new Date(date), outputFormat).toString(); | ||
export function formatDisplayDate(dateString: string): string { | ||
if (!dateString) { | ||
return "Invalid date"; | ||
} | ||
|
||
try { | ||
const date = parseISO(dateString); | ||
return format(date, "do LLL yyyy"); | ||
} catch (error) { | ||
console.error("Error parsing date:", error); | ||
return "Invalid date"; | ||
} | ||
} |