mirror of
https://github.com/superseriousbusiness/gotosocial
synced 2025-06-05 21:59:39 +02:00
[chore/performance] Reuse Intl.DateTimeFormat for formatting times (#4013)
This commit is contained in:
@@ -315,22 +315,26 @@ function inLightbox(element) {
|
|||||||
lightbox.pswp.currSlide.data.attachmentId;
|
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 => {
|
Array.from(document.getElementsByTagName('time')).forEach(timeTag => {
|
||||||
const datetime = timeTag.getAttribute('datetime');
|
const datetime = timeTag.getAttribute('datetime');
|
||||||
const currentText = timeTag.textContent.trim();
|
const currentText = timeTag.textContent.trim();
|
||||||
// Only format if current text contains precise time.
|
// Only format if current text contains precise time.
|
||||||
if (currentText.match(/\d{2}:\d{2}/)) {
|
if (currentText.match(/\d{2}:\d{2}/)) {
|
||||||
const date = new Date(datetime);
|
const date = new Date(datetime);
|
||||||
timeTag.textContent = date.toLocaleString(
|
timeTag.textContent = dateTimeFormat.format(date);
|
||||||
undefined,
|
|
||||||
{
|
|
||||||
year: 'numeric',
|
|
||||||
month: 'short',
|
|
||||||
day: '2-digit',
|
|
||||||
hour: '2-digit',
|
|
||||||
minute: '2-digit',
|
|
||||||
hour12: false
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@@ -18,7 +18,7 @@
|
|||||||
*/ -}}
|
*/ -}}
|
||||||
|
|
||||||
{{- define "ariaLabel" -}}
|
{{- define "ariaLabel" -}}
|
||||||
@{{ .Account.Acct -}}, {{ .CreatedAt | timestampPrecise -}} (server time)
|
@{{ .Account.Acct -}}, {{ .CreatedAt | timestampPrecise }} (server time)
|
||||||
{{- if .LanguageTag -}}
|
{{- if .LanguageTag -}}
|
||||||
, language {{ .LanguageTag.DisplayStr -}}
|
, language {{ .LanguageTag.DisplayStr -}}
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
|
Reference in New Issue
Block a user