From d7004bc3b58c51c2b6444d2088d1e65615be8981 Mon Sep 17 00:00:00 2001 From: Adorian Doran Date: Tue, 10 Dec 2024 18:40:24 +0200 Subject: [PATCH] client: allow date & time to be passed as a string to the date & time formatter, refactor --- src/public/app/utils/formatters.js | 22 ++++++++++++++----- src/public/app/widgets/dialogs/about.js | 4 ++-- .../app/widgets/dialogs/recent_changes.js | 6 ++--- .../ribbon_widgets/note_info_widget.js | 6 ++--- .../widgets/type_widgets/options/backup.js | 4 ++-- .../app/widgets/type_widgets/options/etapi.js | 4 ++-- 6 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/public/app/utils/formatters.js b/src/public/app/utils/formatters.js index 5e3473d8d..be89f0649 100644 --- a/src/public/app/utils/formatters.js +++ b/src/public/app/utils/formatters.js @@ -1,22 +1,32 @@ /** - * Formats the given date to a string based on the current locale. - * @param {Date | number} date + * Formats the given date and time to a string based on the current locale. + * @param {string | Date | number} date * @param {"full" | "long" | "medium" | "short" | "none" | undefined} dateStyle * @param {"full" | "long" | "medium" | "short" | "none" | undefined} tiemStyle */ -export function formatDate(date, dateStyle = "medium", timeStyle = "medium") { +export function formatDateTime(date, dateStyle = "medium", timeStyle = "medium") { const locale = navigator.language; + let parsedDate; + if (typeof date === "string") { + // Parse the given string as a date + parsedDate = new Date(date); + } else if (typeof date === "number" || date instanceof Date) { + parsedDate = date; + } else { + throw new TypeError(); + }; + if (timeStyle === "none") { // Format only the date - return date.toLocaleDateString(locale, {dateStyle}); + return parsedDate.toLocaleDateString(locale, {dateStyle}); } else if (dateStyle === "none") { // Format only the time - return date.toLocaleTimeString(locale, {timeStyle}); + return parsedDate.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(parsedDate); } } diff --git a/src/public/app/widgets/dialogs/about.js b/src/public/app/widgets/dialogs/about.js index 942487ca3..d58cb1091 100644 --- a/src/public/app/widgets/dialogs/about.js +++ b/src/public/app/widgets/dialogs/about.js @@ -1,4 +1,4 @@ -import { formatDate } from "../../utils/formatters.js" +import { formatDateTime } from "../../utils/formatters.js" import { t } from "../../services/i18n.js"; import BasicWidget from "../basic_widget.js"; import openService from "../../services/open.js"; @@ -69,7 +69,7 @@ export default class AboutDialog extends BasicWidget { this.$appVersion.text(appInfo.appVersion); this.$dbVersion.text(appInfo.dbVersion); this.$syncVersion.text(appInfo.syncVersion); - this.$buildDate.text(formatDate(new Date(appInfo.buildDate))); + this.$buildDate.text(formatDateTime(appInfo.buildDate)); this.$buildRevision.text(appInfo.buildRevision); this.$buildRevision.attr('href', `https://github.com/TriliumNext/Notes/commit/${appInfo.buildRevision}`); if (utils.isElectron()) { diff --git a/src/public/app/widgets/dialogs/recent_changes.js b/src/public/app/widgets/dialogs/recent_changes.js index 7a854d81e..515fdfaa5 100644 --- a/src/public/app/widgets/dialogs/recent_changes.js +++ b/src/public/app/widgets/dialogs/recent_changes.js @@ -1,4 +1,4 @@ -import { formatDate } from "../../utils/formatters.js" +import { formatDateTime } from "../../utils/formatters.js" import { t } from "../../services/i18n.js"; import appContext from "../../components/app_context.js"; import BasicWidget from "../basic_widget.js"; @@ -72,11 +72,11 @@ export default class RecentChangesDialog extends BasicWidget { for (const [dateDay, dayChanges] of groupedByDate) { const $changesList = $('