fix: fix relative date issue
This commit is contained in:
1 parent
c4a1f143cb
commit
406792adce
2 files changed
+27
-24
No files matched your search
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "photon-lemmy",
|
"name": "photon-lemmy",
|
||||||
"version": "1.2.2",
|
"version": "1.2.3",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite dev",
|
"dev": "vite dev",
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user