Skip to content

Commit b13eeb8

Browse files
authored
feat: format DateTime title (#331)
1 parent 8123b76 commit b13eeb8

2 files changed

Lines changed: 18 additions & 8 deletions

File tree

app/components/DateTime.vue

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,20 @@ const { locale } = useI18n()
3333
3434
const relativeDates = useRelativeDates()
3535
36+
const dateFormatter = new Intl.DateTimeFormat(locale.value, {
37+
month: 'short',
38+
day: 'numeric',
39+
year: 'numeric',
40+
hour: 'numeric',
41+
minute: '2-digit',
42+
timeZoneName: 'short',
43+
})
44+
3645
// Compute the title - always show full date for accessibility
3746
const titleValue = computed(() => {
3847
if (props.title) return props.title
39-
if (typeof props.datetime === 'string') return props.datetime
40-
return props.datetime.toISOString()
48+
const date = typeof props.datetime === 'string' ? new Date(props.datetime) : props.datetime
49+
return dateFormatter.format(date)
4150
})
4251
</script>
4352

test/nuxt/components/DateTime.spec.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ vi.mock('~/composables/useSettings', () => ({
1616
describe('DateTime', () => {
1717
const testDate = '2024-01-15T12:00:00.000Z'
1818
const testDateObject = new Date('2024-06-15T10:30:00.000Z')
19+
const testYear = testDateObject.getUTCFullYear()
1920

2021
beforeEach(() => {
2122
mockRelativeDates.value = false
@@ -61,12 +62,12 @@ describe('DateTime', () => {
6162
})
6263

6364
describe('title attribute', () => {
64-
it('uses datetime string as title by default', async () => {
65+
it('has title with formatted date by default', async () => {
6566
const component = await mountSuspended(DateTime, {
6667
props: { datetime: testDate },
6768
})
6869
const timeEl = component.find('time')
69-
expect(timeEl.attributes('title')).toBe(testDate)
70+
expect(timeEl.attributes('title')).toContain(testYear)
7071
})
7172

7273
it('uses custom title when provided', async () => {
@@ -81,12 +82,12 @@ describe('DateTime', () => {
8182
expect(timeEl.attributes('title')).toBe(customTitle)
8283
})
8384

84-
it('converts Date object to ISO string for title', async () => {
85+
it('converts Date object to formatted date in title', async () => {
8586
const component = await mountSuspended(DateTime, {
8687
props: { datetime: testDateObject },
8788
})
8889
const timeEl = component.find('time')
89-
expect(timeEl.attributes('title')).toBe(testDateObject.toISOString())
90+
expect(timeEl.attributes('title')).toContain(testYear)
9091
})
9192
})
9293

@@ -119,14 +120,14 @@ describe('DateTime', () => {
119120
let component = await mountSuspended(DateTime, {
120121
props: { datetime: testDate },
121122
})
122-
expect(component.find('time').attributes('title')).toBe(testDate)
123+
expect(component.find('time').attributes('title')).toContain(testYear)
123124

124125
// Test with relative dates on
125126
mockRelativeDates.value = true
126127
component = await mountSuspended(DateTime, {
127128
props: { datetime: testDate },
128129
})
129-
expect(component.find('time').attributes('title')).toBe(testDate)
130+
expect(component.find('time').attributes('title')).toContain(testYear)
130131
})
131132
})
132133

0 commit comments

Comments
 (0)