Merge branch 'main' into mono-ui

This commit is contained in:
Xylight authored and GitHub committed 2023-09-03 20:07:29 -07:00
commit 819fde66ab
1 file changed
+26 -23
+26 -23
View File
@@ -7,36 +7,39 @@
} }
function formatRelativeDate(date: Date) { function formatRelativeDate(date: Date) {
const now = relativeTo?.getTime() ?? Date.now() try {
const now = relativeTo?.getTime() ?? Date.now()
const diffInMillis = now - date.getTime() const diffInMillis = now - date.getTime()
const thresholds = [ const thresholds = [
{ unit: 'second', threshold: 1000 }, { unit: 'second', threshold: 1000 },
{ unit: 'minute', threshold: 60 * 1000 }, { unit: 'minute', threshold: 60 * 1000 },
{ unit: 'hour', threshold: 60 * 60 * 1000 }, { unit: 'hour', threshold: 60 * 60 * 1000 },
{ unit: 'day', threshold: 24 * 60 * 60 * 1000 }, { unit: 'day', threshold: 24 * 60 * 60 * 1000 },
{ unit: 'week', threshold: 7 * 24 * 60 * 60 * 1000 }, { unit: 'week', threshold: 7 * 24 * 60 * 60 * 1000 },
{ unit: 'month', threshold: 30 * 24 * 60 * 60 * 1000 }, { unit: 'month', threshold: 30 * 24 * 60 * 60 * 1000 },
{ unit: 'year', threshold: 365 * 24 * 60 * 60 * 1000 }, { unit: 'year', threshold: 365 * 24 * 60 * 60 * 1000 },
] ]
for (let i = thresholds.length - 1; i >= 0; i--) { for (let i = thresholds.length - 1; i >= 0; i--) {
if (diffInMillis >= thresholds[i].threshold) { if (diffInMillis >= thresholds[i].threshold) {
const value = Math.round(diffInMillis / thresholds[i].threshold) const value = Math.round(diffInMillis / thresholds[i].threshold)
let language = 'en' let language = 'en'
if (typeof navigator != 'undefined') { if (typeof navigator != 'undefined') {
language = navigator.language language = navigator.language
}
const rtf = new Intl.RelativeTimeFormat(language, options)
return rtf.format(-value, thresholds[i].unit as any)
} }
const rtf = new Intl.RelativeTimeFormat(language, options)
return rtf.format(-value, thresholds[i].unit as any)
} }
return 'Now'
} catch (err) {
return 'Now'
} }
return 'Now'
} }
</script> </script>