Skip to content

Commit

Permalink
Create util to transform grant timestamp to time ago text format
Browse files Browse the repository at this point in the history
  • Loading branch information
Pabl0cks committed Feb 26, 2024
1 parent ce92082 commit a50557f
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions packages/nextjs/utils/grants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,32 @@ export const PROPOSAL_STATUS = {
} as const;

export type ProposalStatusType = (typeof PROPOSAL_STATUS)[keyof typeof PROPOSAL_STATUS];

export function formatDateFromNow(timestamp: number) {
const timestampDate = new Date(timestamp);
const now = new Date().getTime();
const delta = now - timestampDate.getTime();
// Number of days calculated from the given delta.
const days = delta / (1000 * 60 * 60 * 24);

// less than 14 days => n days ago
if (days < 14) {
const roundedDays = Math.floor(days);
return `${roundedDays} day${roundedDays > 1 ? "s" : ""} ago`;
}
// Less than 8 weeks => n weeks ago
else if (days < 8 * 7) {
const weeks = Math.floor(days / 7);
return `${weeks} week${weeks > 1 ? "s" : ""} ago`;
}
// Less than 24 months => n months ago
else if (days < 24 * 30) {
const months = Math.floor(days / 30);
return `${months} month${months > 1 ? "s" : ""} ago`;
}
// More than 24 months => n years ago
else {
const years = Math.floor(days / 365);
return `${years} year${years > 1 ? "s" : ""} ago`;
}
}

0 comments on commit a50557f

Please sign in to comment.