跳到内容

useDateFormat

类别
导出大小
932 B
最后更改
5 天前

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

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

格式输出描述
Yo2018th序数格式化的年份
YY18两位数年份
YYYY2018四位数年份
M1-12月份,从 1 开始
Mo1st, 2nd, ..., 12th月份,序数格式化
MM01-12月份,2 位数
MMMJan-Dec月份缩写名称
MMMMJanuary-December月份完整名称
D1-31月份中的日期
Do1st, 2nd, ..., 31st月份中的日期,序数格式化
DD01-31月份中的日期,2 位数
H0-23小时
Ho0th, 1st, 2nd, ..., 23rd小时,序数格式化
HH00-23小时,2 位数
h1-12小时,12 小时制
ho1st, 2nd, ..., 12th小时,12 小时制
hh01-12小时,12 小时制,2 位数
m0-59分钟
mo0th, 1st, ..., 59th分钟,序数格式化
mm00-59分钟,2 位数
s0-59
so0th, 1st, ..., 59th秒,序数格式化
ss00-59秒,2 位数
SSS000-999毫秒,3 位数
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 来自定义。

演示

Monday 2025-03-10 03:47:06

格式化编辑器

用法

基础

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>

源码

SourceDemoDocs

贡献者

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

更新日志

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

在 MIT 许可证下发布。