Fix Date component for string from remark

This commit is contained in:
thiloho
2025-04-26 09:43:01 +02:00
parent 69be9d8ab7
commit f8e71fc3b6
3 changed files with 26 additions and 12 deletions

View File

@@ -1,17 +1,29 @@
---
interface Props {
date: Date;
date: Date | string;
}
const { date } = Astro.props;
const isStringDate = typeof date === "string";
const transformedDate = isStringDate ? new Date(date) : date;
// Create the base options object
const localeOptions = {
year: "numeric",
month: "long",
day: "numeric",
};
// Conditionally add time options if it's a string date
if (isStringDate) {
Object.assign(localeOptions, {
hour: "2-digit",
minute: "2-digit"
});
}
---
<time datetime={date.toISOString()}>
{
date.toLocaleString("en-us", {
year: "numeric",
month: "long",
day: "numeric",
})
}
</time>
<time datetime={transformedDate.toISOString()}>
{transformedDate.toLocaleString("en-us", localeOptions)}
</time>