Skip to content

Commit 4a7a8ed

Browse files
OrbisKantfu
andauthored
feat(useDateFormat): locales is now reactive (#3907)
Co-authored-by: Anthony Fu <[email protected]>
1 parent 7b1082c commit 4a7a8ed

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

packages/shared/useDateFormat/demo.vue

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,36 @@
22
import { ref } from 'vue'
33
import { useDateFormat, useNow } from '@vueuse/core'
44
5-
const formatter = ref('YYYY-MM-DD HH:mm:ss')
6-
const formatted = useDateFormat(useNow(), formatter)
5+
const formatter = ref('dddd YYYY-MM-DD HH:mm:ss')
6+
const lang = ref('en-US')
7+
const formatted = useDateFormat(useNow(), formatter, { locales: lang })
78
</script>
89

910
<template>
1011
<p class="text-20px font-bold text-emerald-500">
1112
{{ formatted }}
1213
</p>
13-
<div class="flex items-center">
14+
<p class="text-20px font-bold text-emerald-500">
15+
{{ locale }}
16+
</p>
17+
<div class="flex flex-col">
1418
<span class="mr-5px text-18px">
1519
Formatter Editor :
1620
</span>
1721
<input v-model="formatter" type="text">
22+
<div space-x-4>
23+
<label class="radio">
24+
<input v-model="lang" value="en-US" type="radio">
25+
<span>English (US)</span>
26+
</label>
27+
<label class="radio">
28+
<input v-model="lang" value="fr" type="radio">
29+
<span>French</span>
30+
</label>
31+
<label class="radio">
32+
<input v-model="lang" value="de" type="radio">
33+
<span>German</span>
34+
</label>
35+
</div>
1836
</div>
1937
</template>

packages/shared/useDateFormat/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface UseDateFormatOptions {
1010
*
1111
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument).
1212
*/
13-
locales?: Intl.LocalesArgument
13+
locales?: MaybeRefOrGetter<Intl.LocalesArgument>
1414

1515
/**
1616
* A custom function to re-modify the way to display meridiem
@@ -53,8 +53,8 @@ export function formatDate(date: Date, formatStr: string, options: UseDateFormat
5353
M: () => month + 1,
5454
Mo: () => formatOrdinal(month + 1),
5555
MM: () => `${month + 1}`.padStart(2, '0'),
56-
MMM: () => date.toLocaleDateString(options.locales, { month: 'short' }),
57-
MMMM: () => date.toLocaleDateString(options.locales, { month: 'long' }),
56+
MMM: () => date.toLocaleDateString(toValue(options.locales), { month: 'short' }),
57+
MMMM: () => date.toLocaleDateString(toValue(options.locales), { month: 'long' }),
5858
D: () => String(days),
5959
Do: () => formatOrdinal(days),
6060
DD: () => `${days}`.padStart(2, '0'),
@@ -72,9 +72,9 @@ export function formatDate(date: Date, formatStr: string, options: UseDateFormat
7272
ss: () => `${seconds}`.padStart(2, '0'),
7373
SSS: () => `${milliseconds}`.padStart(3, '0'),
7474
d: () => day,
75-
dd: () => date.toLocaleDateString(options.locales, { weekday: 'narrow' }),
76-
ddd: () => date.toLocaleDateString(options.locales, { weekday: 'short' }),
77-
dddd: () => date.toLocaleDateString(options.locales, { weekday: 'long' }),
75+
dd: () => date.toLocaleDateString(toValue(options.locales), { weekday: 'narrow' }),
76+
ddd: () => date.toLocaleDateString(toValue(options.locales), { weekday: 'short' }),
77+
dddd: () => date.toLocaleDateString(toValue(options.locales), { weekday: 'long' }),
7878
A: () => meridiem(hours, minutes),
7979
AA: () => meridiem(hours, minutes, false, true),
8080
a: () => meridiem(hours, minutes, true),

0 commit comments

Comments
 (0)