mirror of
https://github.com/p-stream/p-stream.git
synced 2026-01-11 20:10:32 +00:00
add days ago counter
This commit is contained in:
parent
13f3bd4f87
commit
843c1b0895
1 changed files with 37 additions and 1 deletions
|
|
@ -52,16 +52,52 @@ export const getSourceName = (feedUrl: string): string => {
|
|||
}
|
||||
};
|
||||
|
||||
const getRelativeTimeString = (date: Date): string => {
|
||||
const now = new Date();
|
||||
const diffInMs = now.getTime() - date.getTime();
|
||||
const diffInDays = Math.floor(diffInMs / (1000 * 60 * 60 * 24));
|
||||
|
||||
if (diffInDays === 0) {
|
||||
const diffInHours = Math.floor(diffInMs / (1000 * 60 * 60));
|
||||
if (diffInHours === 0) {
|
||||
const diffInMinutes = Math.floor(diffInMs / (1000 * 60));
|
||||
if (diffInMinutes === 0) {
|
||||
return "just now";
|
||||
}
|
||||
return `${diffInMinutes} minute${diffInMinutes !== 1 ? "s" : ""} ago`;
|
||||
}
|
||||
return `${diffInHours} hour${diffInHours !== 1 ? "s" : ""} ago`;
|
||||
}
|
||||
if (diffInDays === 1) {
|
||||
return "1 day ago";
|
||||
}
|
||||
if (diffInDays < 7) {
|
||||
return `${diffInDays} days ago`;
|
||||
}
|
||||
if (diffInDays < 30) {
|
||||
const weeks = Math.floor(diffInDays / 7);
|
||||
return `${weeks} week${weeks !== 1 ? "s" : ""} ago`;
|
||||
}
|
||||
if (diffInDays < 365) {
|
||||
const months = Math.floor(diffInDays / 30);
|
||||
return `${months} month${months !== 1 ? "s" : ""} ago`;
|
||||
}
|
||||
const years = Math.floor(diffInDays / 365);
|
||||
return `${years} year${years !== 1 ? "s" : ""} ago`;
|
||||
};
|
||||
|
||||
export const formatDate = (dateString: string) => {
|
||||
try {
|
||||
const date = new Date(dateString);
|
||||
return date.toLocaleDateString("en-US", {
|
||||
const formattedDate = date.toLocaleDateString("en-US", {
|
||||
year: "numeric",
|
||||
month: "short",
|
||||
day: "numeric",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
});
|
||||
const relativeTime = getRelativeTimeString(date);
|
||||
return `${formattedDate} • ${relativeTime}`;
|
||||
} catch {
|
||||
return dateString;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue