fix: fix relative date issue

This commit is contained in:
Xyphyn committed 2023-09-03 20:03:57 -07:00
1 parent c4a1f143cb
commit 406792adce
2 files changed
+27 -24

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "photon-lemmy",
"version": "1.2.2",
"version": "1.2.3",
"private": true,
"scripts": {
"dev": "vite dev",
+26 -23
View File
@@ -7,36 +7,39 @@
}
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 = [
{ unit: 'second', threshold: 1000 },
{ unit: 'minute', threshold: 60 * 1000 },
{ unit: 'hour', threshold: 60 * 60 * 1000 },
{ unit: 'day', threshold: 24 * 60 * 60 * 1000 },
{ unit: 'week', threshold: 7 * 24 * 60 * 60 * 1000 },
{ unit: 'month', threshold: 30 * 24 * 60 * 60 * 1000 },
{ unit: 'year', threshold: 365 * 24 * 60 * 60 * 1000 },
]
const thresholds = [
{ unit: 'second', threshold: 1000 },
{ unit: 'minute', threshold: 60 * 1000 },
{ unit: 'hour', threshold: 60 * 60 * 1000 },
{ unit: 'day', threshold: 24 * 60 * 60 * 1000 },
{ unit: 'week', threshold: 7 * 24 * 60 * 60 * 1000 },
{ unit: 'month', threshold: 30 * 24 * 60 * 60 * 1000 },
{ unit: 'year', threshold: 365 * 24 * 60 * 60 * 1000 },
]
for (let i = thresholds.length - 1; i >= 0; i--) {
if (diffInMillis >= thresholds[i].threshold) {
const value = Math.round(diffInMillis / thresholds[i].threshold)
for (let i = thresholds.length - 1; i >= 0; i--) {
if (diffInMillis >= thresholds[i].threshold) {
const value = Math.round(diffInMillis / thresholds[i].threshold)
let language = 'en'
if (typeof navigator != 'undefined') {
language = navigator.language
let language = 'en'
if (typeof navigator != 'undefined') {
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>