Skip to content

Commit

Permalink
Make formatElapsedDate more performant.
Browse files Browse the repository at this point in the history
Refactor to make the `formatElapsedDate` slightly more performant.
  • Loading branch information
iamlogand committed Jul 8, 2024
1 parent 633c9cb commit 284e0d0
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions frontend/functions/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,26 @@ export const formatElapsedDate = (date: Date, timezone: string) => {

const seconds = Math.floor(elapsed / 1000)
const minutes = Math.floor(seconds / 60)
const hours = Math.floor(minutes / 60)
const days = Math.floor(hours / 24)
const weeks = Math.floor(days / 7)
const months = Math.floor(days / 30)
const years = Math.floor(days / 365)

if (minutes < 1) {
return "Now"
}
const hours = Math.floor(minutes / 60)
if (hours < 1) {
return `${minutes} ${minutes === 1 ? "min" : "mins"} ago`
}
const days = Math.floor(hours / 24)
if (days < 1) {
return `${hours} ${hours === 1 ? "hour" : "hours"} ago`
}
const weeks = Math.floor(days / 7)
if (weeks < 1) {
return `${days} ${days === 1 ? "day" : "days"} ago`
}
const months = Math.floor(days / 30)
if (months < 1) {
return `${weeks} ${weeks === 1 ? "week" : "weeks"} ago`
}
const years = Math.floor(days / 365)
if (years < 1) {
return `${months} ${months === 1 ? "month" : "months"} ago`
}
Expand Down

0 comments on commit 284e0d0

Please sign in to comment.