Consolidated date comparison to it's own function

This commit is contained in:
Andre Jarrell 2023-05-05 11:11:09 -04:00
parent e1610e38f0
commit 619241c315

View File

@ -23,33 +23,36 @@ export default function Component({ service }) {
); );
} }
const now = new Date()
const address = statsData.addresses[0] const address = statsData.addresses[0]
const now = new Date()
const compareDifferenceInTwoDates = (priorDate, futureDate) => {
const diff = futureDate.getTime() - priorDate.getTime()
const diffInYears = Math.ceil(diff / (1000 * 60 * 60 * 24 * 365))
if (diffInYears > 1) return `${diffInYears}y`
const diffInWeeks = Math.ceil(diff / (1000 * 60 * 60 * 24 * 7))
if (diffInWeeks > 1) return `${diffInWeeks}w`
const diffInDays = Math.ceil(diff / (1000 * 60 * 60 * 24))
if (diffInDays > 1) return `${diffInDays}d`
const diffInHours = Math.ceil(diff / (1000 * 60 * 60))
if (diffInHours > 1) return `${diffInHours}h`
const diffInMinutes = Math.ceil(diff / (1000 * 60))
if (diffInMinutes > 1) return `${diffInMinutes}m`
const diffInSeconds = Math.ceil(diff / 1000)
if (diffInSeconds > 10) return `${diffInSeconds}s`
return 'Now'
}
const getLastSeen = () => { const getLastSeen = () => {
const lastSeen = new Date(statsData.lastSeen) const lastSeen = new Date(statsData.lastSeen)
const diff = now.getTime() - lastSeen.getTime() const diff = compareDifferenceInTwoDates(lastSeen, now)
const diffInYears = Math.ceil(diff / (1000 * 60 * 60 * 24 * 365)) return `${diff === 'Now' ? diff : `${diff} Ago`}`
if (diffInYears > 1) return `${diffInYears}y Ago`
const diffInWeeks = Math.ceil(diff / (1000 * 60 * 60 * 24 * 7))
if (diffInWeeks > 1) return `${diffInWeeks}w Ago`
const diffInDays = Math.ceil(diff / (1000 * 60 * 60 * 24))
if (diffInDays > 1) return `${diffInDays}d Ago`
const diffInHours = Math.ceil(diff / (1000 * 60 * 60))
if (diffInHours > 1) return `${diffInHours}h Ago`
const diffInMinutes = Math.ceil(diff / (1000 * 60))
if (diffInMinutes > 1) return `${diffInMinutes}m Ago`
const diffInSeconds = Math.ceil(diff / 1000)
if (diffInSeconds > 10) return `${diffInSeconds}s Ago`
return 'Just Now'
} }
const getExpiry = () => { const getExpiry = () => {
if (statsData.keyExpiryDisabled) return 'Never' if (statsData.keyExpiryDisabled) return 'Never'
const expiry = new Date(statsData.expires) const expiry = new Date(statsData.expires)
const diff = expiry.getTime() - now.getTime() return compareDifferenceInTwoDates(now, expiry)
const days = Math.ceil(diff / (1000 * 60 * 60 * 24))
return `${days} Days`
} }
return ( return (