initial commit
This commit is contained in:
52
src/components/FormattedDate.astro
Normal file
52
src/components/FormattedDate.astro
Normal file
@@ -0,0 +1,52 @@
|
||||
---
|
||||
interface Props {
|
||||
date: Date | string;
|
||||
includeRelative?: boolean;
|
||||
}
|
||||
|
||||
const { date, includeRelative = false } = Astro.props;
|
||||
|
||||
const formatDate = (date: Date | string, includeRelative: boolean): string => {
|
||||
let currentDate = new Date();
|
||||
let targetDate = date instanceof Date ? date : new Date(date);
|
||||
|
||||
if (isNaN(targetDate.getTime())) {
|
||||
console.error("Invalid date:", date);
|
||||
return "Invalid Date";
|
||||
}
|
||||
|
||||
let yearsAgo = currentDate.getFullYear() - targetDate.getFullYear();
|
||||
let monthsAgo = currentDate.getMonth() - targetDate.getMonth();
|
||||
let daysAgo = currentDate.getDate() - targetDate.getDate();
|
||||
|
||||
let formattedDate = "";
|
||||
|
||||
if (yearsAgo > 0) {
|
||||
formattedDate = `${yearsAgo}y ago`;
|
||||
} else if (monthsAgo > 0) {
|
||||
formattedDate = `${monthsAgo}mo ago`;
|
||||
} else if (daysAgo > 0) {
|
||||
formattedDate = `${daysAgo}d ago`;
|
||||
} else {
|
||||
formattedDate = "Today";
|
||||
}
|
||||
|
||||
let fullDate = targetDate.toLocaleString("en-us", {
|
||||
month: "short",
|
||||
day: "numeric",
|
||||
year: "numeric",
|
||||
});
|
||||
|
||||
if (!includeRelative) {
|
||||
return fullDate;
|
||||
}
|
||||
|
||||
return `${fullDate} (${formattedDate})`;
|
||||
};
|
||||
|
||||
const formattedDateString = formatDate(date, includeRelative);
|
||||
---
|
||||
|
||||
<time datetime={new Date(date).toISOString()}>
|
||||
{formattedDateString}
|
||||
</time>
|
||||
Reference in New Issue
Block a user