misc: Update relative dates

This commit is contained in:
Xyphyn committed 2023-07-06 13:00:20 -07:00
1 parent 4899accfcf
commit e259ceb6e4
5 files changed
+60 -15

No files matched your search

+36 -8
View File
@@ -1,12 +1,40 @@
<script lang="ts">
export let date: Date
const rtf = new Intl.RelativeTimeFormat('en', {
numeric: 'auto',
style: 'short',
}).format(
Math.floor((date.getTime() - Date.now()) / 1000 / 60 / 60 / 24),
'days'
)
function formatRelativeDate(date: Date) {
const now = Date.now()
// Calculate the difference in milliseconds between 'now' and 'date'
const diffInMillis = now - date.getTime()
// Define time thresholds for different units (milliseconds)
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 },
// You can add more units as needed, e.g., week, month, year
]
// Iterate through the thresholds to find the appropriate unit to display
for (let i = thresholds.length - 1; i >= 0; i--) {
if (diffInMillis >= thresholds[i].threshold) {
const value = Math.round(diffInMillis / thresholds[i].threshold)
// Create a RelativeTimeFormat instance with user's locale and options
const rtf = new Intl.RelativeTimeFormat(navigator.language, {
numeric: 'auto',
})
return rtf.format(-value, thresholds[i].unit as any)
}
}
return 'Now' // If none of the above conditions match defaulting to "Now"
}
</script>
{rtf}
{formatRelativeDate(date)}