Skip to content

Commit

Permalink
chore: use date-fns formatting function for timestamps (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
astone123 authored Dec 21, 2023
1 parent a2e600b commit f21db3f
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 16 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@types/react-dom": "^18.0.6",
"axios": "^0.26.1",
"bootstrap": "^5.3.2",
"date-fns": "^3.0.2",
"popper": "^1.0.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
20 changes: 4 additions & 16 deletions src/components/Video.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {formatDistance} from 'date-fns';
import {useEffect, useState} from 'react';
import {API_BASE_URL} from '../lib/api/core';
import {Goal} from '../lib/api/goals';
Expand Down Expand Up @@ -36,23 +37,10 @@ function Video({goal}: {goal: Goal}) {

function formatDateAgo(date: Date): string {
const now = new Date();
const timeDifference = now.getTime() - date.getTime();
const dayDifference = timeDifference / (1000 * 3600 * 24);
const hourDifference = timeDifference / (1000 * 3600);
const minuteDifference = timeDifference / (1000 * 60);

if (dayDifference >= 1) {
const numDays = Math.trunc(dayDifference);
return numDays === 1 ? '1 day ago' : `${numDays} days ago`;
} else if (hourDifference >= 1) {
const numHours = Math.trunc(hourDifference);
return numHours === 1 ? '1 hour ago' : `${numHours} hours ago`;
} else if (minuteDifference >= 1) {
const numMinutes = Math.trunc(minuteDifference);
return numMinutes === 1 ? '1 minute ago' : `${numMinutes} minutes ago`;
} else {
return 'Just now';
}
return formatDistance(date, now, {addSuffix: true})
.replace('about ', '')
.replace('less than a minute ago', 'just now');
}

function getPostId(fullName: string) {
Expand Down

0 comments on commit f21db3f

Please sign in to comment.