Skip to content

useDateFormat

类别
导出体积
972 B
上次更改
1 hour ago

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

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

格式输出描述
Yo2018th序数格式化年份
YY18两位数年份
YYYY2018四位数年份
M1-12月份,从1开始
Mo1st, 2nd, ..., 12th月份,序数格式化
MM01-12月份,两位数
MMMJan-Dec缩写月份名
MMMMJanuary-December完整月份名
D1-31日期
Do1st, 2nd, ..., 31st日期,序数格式化
DD01-31日期,两位数
H0-23小时
Ho0th, 1st, 2nd, ..., 23rd小时,序数格式化
HH00-23小时,两位数
h1-12小时,12小时制
ho1st, 2nd, ..., 12th小时,12小时制,排序
hh01-12小时,12小时制,两位数
m0-59分钟
mo0th, 1st, ..., 59th分钟,序数格式化
mm00-59分钟,两位数
s0-59
so0th, 1st, ..., 59th秒,序数格式化
ss00-59秒,两位数
SSS000-999毫秒,三位数
AAM PM上午下午
AAA.M. P.M.上午下午,周期
aam pm上午下午,小写
aaa.m. p.m.上午下午,小写和周期
d0-6星期几,星期日为0
ddS-S星期几的简写
dddSun-Sat星期几的简写
ddddSunday-Saturday星期几的全名
zGMT, GMT+1带有偏移的时区
zzGMT, GMT+1带有偏移的时区
zzzGMT, GMT+1带有偏移的时区
zzzzGMT, GMT+01:00带有偏移的长时区
  • 可通过在 options 中定义 customMeridiem 来自定义上午下午。

示例

Thursday 2025-11-06 16:01:44

格式化编辑器 :

使用

基本用法

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 { /** * 用于 dd/ddd/dddd/MMM/MMMM 格式的语言环境 * * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument). */
locales
?:
MaybeRefOrGetter
<Intl.
LocalesArgument
>
/** * 重新修改显示上午下午的方式的自定义函数 * */
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>
/** * 根据传入的令牌字符串获取格式化日期。 * * @see https://vueuse.org/useDateFormat * @param date - 要格式化的日期,可以是 `Date` 对象、时间戳或字符串 * @param formatStr - 用于格式化日期的令牌组合 * @param options - UseDateFormatOptions * * @__NO_SIDE_EFFECTS__ */ export declare function
useDateFormat
(
date
:
MaybeRefOrGetter
<
DateLike
>,
formatStr
?:
MaybeRefOrGetter
<string>,
options
?: UseDateFormatOptions,
):
UseDateFormatReturn

源码

源码演示文档

贡献者

一纸忘忧

更新日志

没有最近的更新日志