diff --git a/src/public/app/utils/formatters.js b/src/public/app/utils/formatters.js new file mode 100644 index 000000000..638404c89 --- /dev/null +++ b/src/public/app/utils/formatters.js @@ -0,0 +1,34 @@ +/** + * 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} timeStyle + */ +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) { + // The given date is already a Date instance or a number + parsedDate = date; + } else { + // Invalid type + throw new TypeError(`Invalid type for the "date" argument.`); + }; + + if (timeStyle === "none") { + // Format only the date + return parsedDate.toLocaleDateString(locale, {dateStyle}); + } else if (dateStyle === "none") { + // Format only the time + return parsedDate.toLocaleTimeString(locale, {timeStyle}); + } else { + // Format the date and time + const formatter = new Intl.DateTimeFormat(navigator.language, {dateStyle, timeStyle}); + return formatter.format(parsedDate); + } +} + diff --git a/src/public/app/widgets/dialogs/about.js b/src/public/app/widgets/dialogs/about.js index 794f26dc9..d58cb1091 100644 --- a/src/public/app/widgets/dialogs/about.js +++ b/src/public/app/widgets/dialogs/about.js @@ -1,8 +1,9 @@ -import server from "../../services/server.js"; -import utils from "../../services/utils.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"; +import server from "../../services/server.js"; +import utils from "../../services/utils.js"; const TPL = ` @@ -68,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(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 99b255f5a..515fdfaa5 100644 --- a/src/public/app/widgets/dialogs/recent_changes.js +++ b/src/public/app/widgets/dialogs/recent_changes.js @@ -1,13 +1,14 @@ +import { formatDateTime } from "../../utils/formatters.js" import { t } from "../../services/i18n.js"; -import linkService from '../../services/link.js'; -import utils from '../../services/utils.js'; -import server from '../../services/server.js'; -import froca from "../../services/froca.js"; import appContext from "../../components/app_context.js"; -import hoistedNoteService from "../../services/hoisted_note.js"; import BasicWidget from "../basic_widget.js"; import dialogService from "../../services/dialog.js"; +import froca from "../../services/froca.js"; +import hoistedNoteService from "../../services/hoisted_note.js"; +import linkService from '../../services/link.js'; +import server from '../../services/server.js'; import toastService from "../../services/toast.js"; +import utils from '../../services/utils.js'; import ws from "../../services/ws.js"; const TPL = ` @@ -71,10 +72,11 @@ export default class RecentChangesDialog extends BasicWidget { for (const [dateDay, dayChanges] of groupedByDate) { const $changesList = $('