useDateFormat
根据传入的令牌字符串获取格式化的日期,灵感来自 dayjs。
所有可用格式列表 (默认为 HH:mm:ss)
格式 | 输出 | 描述 |
---|---|---|
Yo | 2018th | 序数格式化的年份 |
YY | 18 | 两位数年份 |
YYYY | 2018 | 四位数年份 |
M | 1-12 | 月份,从 1 开始 |
Mo | 1st, 2nd, ..., 12th | 月份,序数格式化 |
MM | 01-12 | 月份,2 位数 |
MMM | Jan-Dec | 月份缩写名称 |
MMMM | January-December | 月份完整名称 |
D | 1-31 | 月份中的日期 |
Do | 1st, 2nd, ..., 31st | 月份中的日期,序数格式化 |
DD | 01-31 | 月份中的日期,2 位数 |
H | 0-23 | 小时 |
Ho | 0th, 1st, 2nd, ..., 23rd | 小时,序数格式化 |
HH | 00-23 | 小时,2 位数 |
h | 1-12 | 小时,12 小时制 |
ho | 1st, 2nd, ..., 12th | 小时,12 小时制 |
hh | 01-12 | 小时,12 小时制,2 位数 |
m | 0-59 | 分钟 |
mo | 0th, 1st, ..., 59th | 分钟,序数格式化 |
mm | 00-59 | 分钟,2 位数 |
s | 0-59 | 秒 |
so | 0th, 1st, ..., 59th | 秒,序数格式化 |
ss | 00-59 | 秒,2 位数 |
SSS | 000-999 | 毫秒,3 位数 |
A | AM PM | 上午/下午 |
AA | A.M. P.M. | 上午/下午,带句点 |
a | am pm | 上午/下午,小写 |
aa | a.m. p.m. | 上午/下午,小写且带句点 |
d | 0-6 | 星期几,星期日为 0 |
dd | S-S | 星期几的简称 |
ddd | Sun-Sat | 星期几的短名称 |
dddd | Sunday-Saturday | 星期几的名称 |
z | GMT, GMT+1 | 带偏移量的时区 |
zz | GMT, GMT+1 | 带偏移量的时区 |
zzz | GMT, GMT+1 | 带偏移量的时区 |
zzzz | GMT, GMT+01:00 | 带偏移量的长时区 |
- 上午/下午可以通过在
options
中定义customMeridiem
来自定义。
演示
用法
基础
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>
与 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>
与自定义上午/下午一起使用
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>
类型声明
显示类型声明
typescript
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
/**
* 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
*/
export declare function useDateFormat(
date: MaybeRefOrGetter<DateLike>,
formatStr?: MaybeRefOrGetter<string>,
options?: UseDateFormatOptions,
): ComputedRef<string>
export type UseDateFormatReturn = ReturnType<typeof useDateFormat>
源码
贡献者
更新日志
v12.8.0
on 3/5/2025v12.6.0
on 2/14/2025v12.3.0
on 1/2/202559f75
- feat(toValue): deprecate toValue
from @vueuse/shared
in favor of Vue's nativev12.0.0-beta.1
on 11/21/2024v11.0.0-beta.2
on 7/17/2024v10.6.0
on 11/9/2023v10.3.0
on 7/30/2023v10.1.0
on 4/22/2023v10.0.0-beta.4
on 4/13/20234d757
- feat(types)!: rename MaybeComputedRef
to MaybeRefOrGetter
0a72b
- feat(toValue): rename resolveUnref
to toValue