-
-
Notifications
You must be signed in to change notification settings - Fork 425
Expand file tree
/
Copy pathDateTime.vue
More file actions
86 lines (81 loc) · 2.25 KB
/
DateTime.vue
File metadata and controls
86 lines (81 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<script setup lang="ts">
/**
* DateTime component that wraps NuxtTime with settings-aware relative date support.
* Uses the global settings to determine whether to show relative or absolute dates.
*
* Note: When relativeDates setting is enabled, the component switches between
* relative and absolute display based on user preference. The title attribute
* always shows the full date for accessibility.
*/
const props = withDefaults(
defineProps<{
/** The datetime value (ISO string or Date) */
datetime: string | Date
/** Override title (defaults to datetime) */
title?: string
/** Date style for absolute display */
dateStyle?: 'full' | 'long' | 'medium' | 'short'
/** Individual date parts for absolute display (alternative to dateStyle) */
year?: 'numeric' | '2-digit'
month?: 'numeric' | '2-digit' | 'long' | 'short' | 'narrow'
day?: 'numeric' | '2-digit'
}>(),
{
title: undefined,
dateStyle: undefined,
year: undefined,
month: undefined,
day: undefined,
},
)
const { locale } = useI18n()
const relativeDates = useRelativeDatesPreference()
const dateFormatter = new Intl.DateTimeFormat(locale.value, {
month: 'short',
day: 'numeric',
year: 'numeric',
hour: 'numeric',
minute: '2-digit',
timeZoneName: 'short',
})
// Compute the title - always show full date for accessibility
const titleValue = computed(() => {
if (props.title) return props.title
const date = typeof props.datetime === 'string' ? new Date(props.datetime) : props.datetime
return dateFormatter.format(date)
})
</script>
<template>
<span>
<ClientOnly>
<NuxtTime
v-if="relativeDates"
:datetime="datetime"
:title="titleValue"
relative
:locale="locale"
/>
<NuxtTime
v-else
:datetime="datetime"
:title="titleValue"
:date-style="dateStyle"
:year="year"
:month="month"
:day="day"
:locale="locale"
/>
<template #fallback>
<NuxtTime
:datetime="datetime"
:title="titleValue"
:date-style="dateStyle"
:year="year"
:month="month"
:day="day"
:locale="locale"
/>
</template>
</ClientOnly>
</span>
</template>