diff options
| author | 2025-04-15 10:04:47 +0200 | |
|---|---|---|
| committer | 2025-04-15 10:04:47 +0200 | |
| commit | 5b2fe1f9a0757b63dbc328c2493ad4bcfd5e7240 (patch) | |
| tree | 458622dd6d496854cedc7ad48dacd106b84496ae | |
| parent | [bugfix/frontend] Fix photoswipe layout issues, keyboard shortcuts (#4010) (diff) | |
| download | gotosocial-5b2fe1f9a0757b63dbc328c2493ad4bcfd5e7240.tar.xz | |
[chore/performance] Reuse Intl.DateTimeFormat for formatting times (#4013)
| -rw-r--r-- | web/source/frontend/index.js | 26 | ||||
| -rw-r--r-- | web/template/status_attributes.tmpl | 2 |
2 files changed, 16 insertions, 12 deletions
diff --git a/web/source/frontend/index.js b/web/source/frontend/index.js index b9de769bd..860d6d10a 100644 --- a/web/source/frontend/index.js +++ b/web/source/frontend/index.js @@ -315,22 +315,26 @@ function inLightbox(element) { lightbox.pswp.currSlide.data.attachmentId; } +// Define + reuse one DateTimeFormat (cheaper). +const dateTimeFormat = Intl.DateTimeFormat( + undefined, + { + year: 'numeric', + month: 'short', + day: '2-digit', + hour: '2-digit', + minute: '2-digit', + hour12: false + }, +); + +// Reformat time text to browser locale. Array.from(document.getElementsByTagName('time')).forEach(timeTag => { const datetime = timeTag.getAttribute('datetime'); const currentText = timeTag.textContent.trim(); // Only format if current text contains precise time. if (currentText.match(/\d{2}:\d{2}/)) { const date = new Date(datetime); - timeTag.textContent = date.toLocaleString( - undefined, - { - year: 'numeric', - month: 'short', - day: '2-digit', - hour: '2-digit', - minute: '2-digit', - hour12: false - }, - ); + timeTag.textContent = dateTimeFormat.format(date); } }); diff --git a/web/template/status_attributes.tmpl b/web/template/status_attributes.tmpl index a99775d59..15c1d17d3 100644 --- a/web/template/status_attributes.tmpl +++ b/web/template/status_attributes.tmpl @@ -18,7 +18,7 @@ */ -}} {{- define "ariaLabel" -}} -@{{ .Account.Acct -}}, {{ .CreatedAt | timestampPrecise -}} (server time) +@{{ .Account.Acct -}}, {{ .CreatedAt | timestampPrecise }} (server time) {{- if .LanguageTag -}} , language {{ .LanguageTag.DisplayStr -}} {{- end -}} |
