Skip to content

useDateFormat ​

类别
导出大小
988 B
最近修改
7 months ago

根据传入的标记字符串获取格式化日期,灵感来自 dayjs。

🌐 Get the formatted date according to the string of tokens passed in, inspired by dayjs.

所有可用格式列表(默认 HH:mm:ss):

格式输出描述
Yo2018年序数格式的年份
YY18两位数年份
YYYY2018四位数年份
M1-12月份,从1开始
Mo第1、第2、…、第12月份,序数格式
MM01-12月份,两位数字
MMM一月至十二月缩写的月份名称
MMMM一月至十二月完整的月份名称
D1-31月中的日期
Do第1天、第2天、...、第31天当月的日期,按序数格式
DD01-31月中的日期,2位数字
H0-23小时
Ho第0、第1、第2、...、第23小时,序数格式
HH00-23小时,2位数字
h1-12小时,12小时制
ho第一、第二、……、第十二小时,12小时制,排序
hh01-12小时,12小时制,2位数字
m0-59分钟
mo第0、第1、…、第59分钟,序数格式
mm00-59分钟,两位数字
s0-59秒
so第0、第1、…、第59第二,序数格式
ss00-59秒,2位数字
SSS000-999毫秒,3位数字
A上午 下午子午线
AA上午 下午子午线,时间段
a上午 下午上午或下午的小写形式
aa上午 下午上下午标志,小写并带点
d0-6一周中的星期几,星期日为 0
ddS-S星期的最小名称
ddd星期日-星期六星期的简称
dddd星期日-星期六星期名称
zGMT, GMT+1带有偏移量的时区
zzGMT, GMT+1带有偏移量的时区
zzzGMT, GMT+1带有偏移量的时区
zzzzGMT, GMT+01:00带偏移的长时区
  • 通过在 options 中定义 customMeridiem 可以自定义午前/下午标记。

示例 ​

Sunday 2026-09-13 23:20:09

Formatter Editor :

用法 ​

🌐 Usage

基本的 ​

🌐 Basic

vue
<script setup lang="ts">
import { 
useDateFormat
,
useNow
} from '@vueuse/core'
const
formatted
=
useDateFormat
(
useNow
(), 'YYYY-MM-DD HH:mm:ss')
</script> <template> <
div
>{{
formatted
}}</
div
>
</template>

与区域设置一起使用 ​

🌐 Use with locales

vue
<script setup lang="ts">
import { 
useDateFormat
,
useNow
} from '@vueuse/core'
const
formatted
=
useDateFormat
(
useNow
(), 'YYYY-MM-DD (ddd)', {
locales
: 'en-US' })
</script> <template> <
div
>{{
formatted
}}</
div
>
</template>

与自定义子午线一起使用 ​

🌐 Use with custom meridiem

vue
<script setup lang="ts">
import { 
useDateFormat
} from '@vueuse/core'
function
customMeridiem
(
hours
: number,
minutes
: number,
isLowercase
?: boolean,
hasPeriod
?: boolean) {
const
m
=
hours
> 11 ? (
isLowercase
? 'μμ' : 'ΜΜ') : (
isLowercase
? 'πμ' : 'ΠΜ')
return
hasPeriod
?
m
.
split
('').
reduce
((
acc
,
current
) =>
acc
+= `${
current
}.`, '') :
m
} const
am
=
useDateFormat
('2022-01-01 05:05:05', 'hh:mm:ss A', {
customMeridiem
})
// am.value = '05:05:05 ΠΜ' const
pm
=
useDateFormat
('2022-01-01 17:05:05', 'hh:mm:ss AA', {
customMeridiem
})
// pm.value = '05:05:05 Μ.Μ.' </script>