From 1268916ad7839ca147e8cef621ec90c996a7a580 Mon Sep 17 00:00:00 2001 From: Adorian Doran Date: Tue, 10 Dec 2024 18:09:55 +0200 Subject: [PATCH] client: date formatter utility: add the ability to format exclusively dates or times --- src/public/app/utils/formatters.js | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/public/app/utils/formatters.js b/src/public/app/utils/formatters.js index f59e3144f..5e3473d8d 100644 --- a/src/public/app/utils/formatters.js +++ b/src/public/app/utils/formatters.js @@ -1,14 +1,22 @@ /** * Formats the given date to a string based on the current locale. * @param {Date | number} date - * @param {"full" | "long" | "medium" | "short" | undefined} dateStyle - * @param {"full" | "long" | "medium" | "short" | undefined} tiemStyle + * @param {"full" | "long" | "medium" | "short" | "none" | undefined} dateStyle + * @param {"full" | "long" | "medium" | "short" | "none" | undefined} tiemStyle */ -export function formatDate(date, dateStyle = "medium", tiemStyle = "medium") { - const formatter = new Intl.DateTimeFormat(navigator.language, { - dateStyle: "medium", - timeStyle: "medium" - }); +export function formatDate(date, dateStyle = "medium", timeStyle = "medium") { + const locale = navigator.language; + + if (timeStyle === "none") { + // Format only the date + return date.toLocaleDateString(locale, {dateStyle}); + } else if (dateStyle === "none") { + // Format only the time + return date.toLocaleTimeString(locale, {timeStyle}); + } else { + // Format the date and time + const formatter = new Intl.DateTimeFormat(navigator.language, {dateStyle, timeStyle}); + return formatter.format(date); + } +} - return formatter.format(date); -} \ No newline at end of file