version 1
Build and Deploy / test-backend (push) Successful in 1m11s
Build and Deploy / lint-frontend (push) Successful in 21s
Build and Deploy / build-backend (push) Successful in 58s
Build and Deploy / deploy (push) Canceled after 0s
Build and Deploy / build-frontend (push) Canceled after 5m38s
Build and Deploy / test-backend (push) Successful in 1m11s
Build and Deploy / lint-frontend (push) Successful in 21s
Build and Deploy / build-backend (push) Successful in 58s
Build and Deploy / deploy (push) Canceled after 0s
Build and Deploy / build-frontend (push) Canceled after 5m38s
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
const MINUTE = 60_000;
|
||||
const HOUR = 60 * MINUTE;
|
||||
const DAY = 24 * HOUR;
|
||||
const MONTH = 30 * DAY;
|
||||
|
||||
export type RelativeTimeKey =
|
||||
| "now"
|
||||
| "minutes"
|
||||
| "hours"
|
||||
| "yesterday"
|
||||
| "days"
|
||||
| "month"
|
||||
| "months"
|
||||
| "year"
|
||||
| "moreThanYear";
|
||||
|
||||
export type RelativeTimeTranslator = (
|
||||
key: RelativeTimeKey,
|
||||
values?: { count?: number }
|
||||
) => string;
|
||||
|
||||
/**
|
||||
* Formats an ISO date as a localized relative time using the following
|
||||
* thresholds:
|
||||
*
|
||||
* - < 1 minute -> "Now"
|
||||
* - < 1 hour -> "X minutes ago"
|
||||
* - < 24 hours -> "X hours ago"
|
||||
* - 1 day -> "Yesterday"
|
||||
* - < 30 days -> "X days ago"
|
||||
* - 1 month -> "A month ago"
|
||||
* - < 12 months -> "X months ago"
|
||||
* - 12-23 months -> "A year ago"
|
||||
* - >= 24 months -> "More than a year ago"
|
||||
*/
|
||||
export function formatDate(
|
||||
dateString: string,
|
||||
t: RelativeTimeTranslator,
|
||||
now: Date = new Date()
|
||||
): string {
|
||||
const date = new Date(dateString);
|
||||
if (Number.isNaN(date.getTime())) return dateString;
|
||||
|
||||
const diffMs = now.getTime() - date.getTime();
|
||||
|
||||
// Future dates (or the very same moment) are shown as "now".
|
||||
if (diffMs < MINUTE) return t("now");
|
||||
|
||||
const minutes = Math.floor(diffMs / MINUTE);
|
||||
if (minutes < 60) return t("minutes", { count: minutes });
|
||||
|
||||
const hours = Math.floor(diffMs / HOUR);
|
||||
if (hours < 24) return t("hours", { count: hours });
|
||||
|
||||
const days = Math.floor(diffMs / DAY);
|
||||
if (days === 1) return t("yesterday");
|
||||
if (days < 30) return t("days", { count: days });
|
||||
|
||||
const months = Math.floor(diffMs / MONTH);
|
||||
if (months === 1) return t("month");
|
||||
if (months < 12) return t("months", { count: months });
|
||||
|
||||
const years = Math.floor(months / 12);
|
||||
if (years === 1) return t("year");
|
||||
return t("moreThanYear");
|
||||
}
|
||||
Reference in New Issue
Block a user