Skip to content

Commit f8bcb30

Browse files
committed
feat: add Timestamp MDC component for inline date display
1 parent e348c53 commit f8bcb30

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<script setup lang="ts">
2+
const appConfig = useAppConfig();
3+
4+
const props = defineProps<{
5+
date: string;
6+
}>();
7+
8+
const parsedDate = computed(() => {
9+
const dateStr = props.date;
10+
// Handle YYYY-MM-DD format to avoid timezone issues
11+
if (/^\d{4}-\d{2}-\d{2}$/.test(dateStr)) {
12+
const [year, month, day] = dateStr.split("-").map(Number);
13+
return new Date(year, month - 1, day);
14+
}
15+
return new Date(dateStr);
16+
});
17+
18+
const formattedDate = computed(() =>
19+
parsedDate.value.toLocaleDateString(
20+
appConfig.date.locale,
21+
appConfig.date.options,
22+
),
23+
);
24+
25+
const datetimeAttr = computed(() => {
26+
// For date-only inputs, use the original string to avoid timezone drift
27+
if (/^\d{4}-\d{2}-\d{2}$/.test(props.date)) {
28+
return props.date;
29+
}
30+
return parsedDate.value.toISOString();
31+
});
32+
</script>
33+
34+
<template>
35+
<time :datetime="datetimeAttr" :title="datetimeAttr">{{ formattedDate }}</time>
36+
</template>

0 commit comments

Comments
 (0)