Enhance formatTimeAgo function to handle UTC timestamps and future timestamps

This commit is contained in:
2025-10-04 17:52:06 +08:00
parent 0174ac83e9
commit 8a1bd75d74

View File

@@ -133,9 +133,13 @@ function App() {
return hours.toLocaleString()
}
// Format time ago from timestamp
// Format time ago from timestamp (API returns UTC timestamps)
const formatTimeAgo = (timestamp: number) => {
const seconds = Math.floor((Date.now() - timestamp) / 1000)
const now = Date.now() // Current local time
const utcNow = now - (new Date().getTimezoneOffset() * 60000) // Convert to UTC
const seconds = Math.floor((utcNow - timestamp) / 1000)
if (seconds < 0) return 'just now' // Future timestamp
if (seconds < 60) return `${seconds}s ago`
const minutes = Math.floor(seconds / 60)
if (minutes < 60) return `${minutes}m ago`