diff --git a/src/components/Card.astro b/src/components/Card.astro
index 39f2321..558953e 100644
--- a/src/components/Card.astro
+++ b/src/components/Card.astro
@@ -1,6 +1,6 @@
---
import { Image } from 'astro:assets'
-import FormattedYear from './FormattedYear.astro'
+import FormattedDate from './FormattedDate.astro'
import Link from './Link.astro'
import { useTranslations } from '@/i18n'
@@ -9,14 +9,14 @@ const t = useTranslations()
interface Props {
index?: number // The index of the card in the list.
title: string
- year?: number | string
+ date?: number | string
description: string
imgSrc?: ImageMetadata | string // @todo Fix the error message.
imgType?: string
href?: string
}
-const { index, title, year, description, imgSrc, imgType, href } = Astro.props
+const { index, title, date, description, imgSrc, imgType, href } = Astro.props
---
- {year && (
+ {date && (
- {t('components.cards.publishedIn')}
-
-
+
)}
@@ -80,11 +80,11 @@ const { index, title, year, description, imgSrc, imgType, href } = Astro.props
)
}
- {!imgSrc &&year && (
+ {!imgSrc && date && (
- {t('components.cards.publishedIn')}
-
-
+
)}
diff --git a/src/components/FormattedDate.astro b/src/components/FormattedDate.astro
index 4efcbd4..c1c7231 100644
--- a/src/components/FormattedDate.astro
+++ b/src/components/FormattedDate.astro
@@ -4,12 +4,52 @@ import { useTranslations } from '@/i18n'
const t = useTranslations()
interface Props {
- date: Date
+ date: Date | number | string
}
const { date } = Astro.props
+
+const locale = t('siteMetadata.language')
+let numericDate = ''
+let localisedDate = ''
+
+if (date instanceof Date) {
+ numericDate = date.toISOString().substring(0, 10)
+ localisedDate = date.toLocaleDateString(locale, {
+ dateStyle: 'long',
+ })
+} else if (typeof date === 'number') {
+ numericDate = String(date)
+ localisedDate = new Date(date, 0, 1).toLocaleDateString(locale, {
+ year: 'numeric',
+ })
+} else if (typeof date === 'string') {
+ let [year, month, day] = date.split('-').map((s) => parseInt(s, 10))
+ if (typeof month === 'number') {
+ month--
+ }
+
+ const options: Intl.DateTimeFormatOptions = {
+ year: 'numeric',
+ }
+ let substring = 4
+
+ if (typeof month === 'number') {
+ options['month'] = 'long'
+ substring = 7
+ }
+
+ if (typeof day === 'number') {
+ options['day'] = 'numeric'
+ substring = 10
+ }
+
+ const dateObj = new Date(year, month || 0, day || 1)
+ numericDate = dateObj.toISOString().substring(0, substring)
+ localisedDate = dateObj.toLocaleDateString(locale, options)
+}
---
-