Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Commit

Permalink
relative time to publishedAt
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerwooo committed Apr 28, 2022
1 parent 8470130 commit 6a32eff
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion Docs/components/ReleaseInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ const reactionToEmoji = {
} as const
type reactionKeys = keyof typeof reactionToEmoji

const formatRelativeDate = (publishedAt: string) => {
const publishedDate = new Date(publishedAt)
const deltaTime = (publishedDate.getTime() - Date.now()) / 1000

const formatter = new Intl.RelativeTimeFormat()
if (deltaTime > -60 * 60) {
return formatter.format(Math.floor(deltaTime / 60), 'minute')
}
if (deltaTime > -24 * 60 * 60) {
return formatter.format(Math.floor(deltaTime / 60 / 60), 'hour')
}
if (deltaTime > -7 * 24 * 60 * 60) {
return formatter.format(Math.floor(deltaTime / 60 / 60 / 24), 'day')
}
return formatter.format(Math.floor(deltaTime / 60 / 60 / 24 / 7), 'week')
}

const ReleaseInfo = ({
htmlUrl,
publishedAt,
Expand All @@ -29,7 +46,7 @@ const ReleaseInfo = ({

return (
<div className="flex flex-wrap items-center text-xs gap-x-2 py-2 opacity-60 hover:opacity-80 transition-all duration-150">
<span>on {new Date(publishedAt).toLocaleDateString()},</span>
<span>{formatRelativeDate(publishedAt)},</span>
<span>
{downloadCount} {downloadCount > 1 ? 'downloads' : 'download'},
</span>
Expand Down

1 comment on commit 6a32eff

@vercel
Copy link

@vercel vercel bot commented on 6a32eff Apr 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.