Skip to content

useDateFormat

类别
导出体积
932 B
上次更改
4 weeks 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 来自定义上午下午。

示例

Wednesday 2025-05-07 22:03:03

格式化编辑器 :

使用

基本用法

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>

类型声明

显示类型声明
typescript
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
 */
export declare function useDateFormat(
  date: MaybeRefOrGetter<DateLike>,
  formatStr?: MaybeRefOrGetter<string>,
  options?: UseDateFormatOptions,
): UseDateFormatReturn

源码

源码演示文档

贡献者

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

更新日志

v13.1.0 on
c1d6e - feat(shared): ensure return types exists (#4659)
v12.8.0 on
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
v12.6.0 on
cd6d7 - feat: add z...zzzz for timezone information (#4553)
v12.3.0 on
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v11.0.0-beta.2 on
4a7a8 - feat: locales is now reactive (#3907)
v10.6.0 on
61ceb - feat: add date ordinal formatting (#3474)
v10.3.0 on
d6428 - fix: handle zero properly (#3272)
v10.1.0 on
a2147 - fix: Error if Y or YYY provided (#3001)
v10.0.0-beta.4 on
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
0a72b - feat(toValue): rename resolveUnref to toValue