Skip to content

Commit

Permalink
Refactor for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
ghengeveld committed Dec 12, 2024
1 parent 6c9d375 commit 508c825
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions code/addons/test/src/components/RelativeTime.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { useCallback, useEffect, useState } from 'react';

export function getRelativeTimeString(date: Date): string {
const delta = Math.round((Date.now() - date.getTime()) / 1000);
if (delta < 60) {
const seconds = Math.round((Date.now() - date.getTime()) / 1000);
if (seconds < 60) {
return 'just now';
}
if (delta < 60 * 60) {
const minutes = Math.floor(delta / 60);

const minutes = Math.floor(seconds / 60);
if (minutes < 60) {
return minutes === 1 ? 'a minute ago' : `${minutes} minutes ago`;
}
if (delta < 60 * 60 * 24) {
const hours = Math.floor(delta / 60 / 60);

const hours = Math.floor(minutes / 60);
if (hours < 24) {
return hours === 1 ? 'an hour ago' : `${hours} hours ago`;
}
const days = Math.floor(delta / 60 / 60 / 24);

const days = Math.floor(hours / 24);
return days === 1 ? 'yesterday' : `${days} days ago`;
}

Expand Down

0 comments on commit 508c825

Please sign in to comment.