跳到主要内容

useDateFormat

分类
导出大小
972 B
上次更改
3 个月前

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

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

格式输出描述
Yo2018年序数格式年份
YY18两位数年份
YYYY2018四位数年份
M1-12月份,从1开始
Mo1日, 2日, ..., 12日月份,序数格式
MM01-12月份,两位数
MMM一月-十二月月份缩写
MMMM一月-十二月月份全称
D1-31月份中的日期
Do1日, 2日, ..., 31日月份中的日期,序数格式
DD01-31月份中的日期,两位数
H0-23小时
Ho0点, 1点, 2点, ..., 23点小时,序数格式
HH00-23小时,两位数
h1-12小时,12小时制
ho1日, 2日, ..., 12日小时,12小时制,序数格式
hh01-12小时,12小时制,两位数
m0-59分钟
mo0分, 1分, ..., 59分分钟,序数格式
mm00-59分钟,两位数
s0-59
so0分, 1分, ..., 59分秒,序数格式
ss00-59秒,两位数
SSS000-999毫秒,三位数
AAM PM上午/下午
AAA.M. P.M.上午/下午,带点
aam pm上午/下午,小写
aaa.m. p.m.上午/下午,小写带点
d0-6星期几,周日为0
dd日-六星期几的缩写名称
ddd周日-周六星期几的短名称
dddd星期日-星期六星期几的名称
zGMT, GMT+1带偏移量的时区
zzGMT, GMT+1带偏移量的时区
zzzGMT, GMT+1带偏移量的时区
zzzzGMT, GMT+01:00带偏移量的完整时区
  • 通过在 options 中定义 customMeridiem 可以自定义上午/下午。

演示

星期四 2025-10-23 06:49:19

格式化编辑器

用法

基本用法

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>

与语言环境一起使用

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>

与自定义上午/下午一起使用

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>

类型声明

显示类型声明
ts
export type 
DateLike
= Date | number | string | undefined
export interface UseDateFormatOptions { /** * The locale(s) to used for dd/ddd/dddd/MMM/MMMM format * * [MDN](https://mdn.org.cn/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument). */
locales
?:
MaybeRefOrGetter
<Intl.
LocalesArgument
>
/** * A custom function to re-modify the way to display meridiem * */
customMeridiem
?: (
hours
: number,
minutes
: number,
isLowercase
?: boolean,
hasPeriod
?: boolean,
) => string } export declare function
formatDate
(
date
: Date,
formatStr
: string,
options
?: UseDateFormatOptions,
): string export declare function
normalizeDate
(
date
:
DateLike
): Date
export type
UseDateFormatReturn
=
ComputedRef
<string>
/** * Get the formatted date according to the string of tokens passed in. * * @see https://vueuse.org.cn/useDateFormat * @param date - The date to format, can either be a `Date` object, a timestamp, or a string * @param formatStr - The combination of tokens to format the date * @param options - UseDateFormatOptions * * @__NO_SIDE_EFFECTS__ */ export declare function
useDateFormat
(
date
:
MaybeRefOrGetter
<
DateLike
>,
formatStr
?:
MaybeRefOrGetter
<string>,
options
?: UseDateFormatOptions,
):
UseDateFormatReturn

来源

源代码演示文档

贡献者

Anthony Fu
Anthony Fu
Robin
Robin
webfansplz
NoiseFan
SerKo
IlyaL
IlyaL
Poet
Dachen
Dino Čamdžić
丶远方
sun0day
Levi (Nguyễn Lương Huy)
Vasya Ponomarenko
aki77
Black

更新日志

v13.6.0
d32f8 - refactor: 为所有纯函数添加 @__NO_SIDE_EFFECTS__ 注释 (#4907)
v13.1.0
c1d6e - feat(shared): 确保返回类型存在 (#4659)
v12.8.0
7432f - feat(types): 废弃 MaybeRefMaybeRefOrGetter,转而使用 Vue 的原生类型 (#4636)
v12.6.0
cd6d7 - feat: 添加 z...zzzz 用于时区信息 (#4553)
v12.3.0
59f75 - feat(toValue): 废弃 @vueuse/shared 中的 toValue,转而使用 Vue 的原生函数
0a9ed - feat!: 放弃对 Vue 2 的支持,优化打包并清理 (#4349)
4a7a8 - feat: 语言环境现在具有响应性 (#3907)
v10.6.0
61ceb - feat: 添加日期序数格式化 (#3474)
v10.3.0
d6428 - fix: 正确处理零值 (#3272)
v10.1.0
a2147 - fix: 如果提供了 Y 或 YYY 则报错 (#3001)

根据 MIT 许可证发布。