跳到主要內容

useDateFormat

Category
匯出大小
932 B
Last Changed
5 days ago

Get the formatted date according to the string of tokens passed in, inspired by dayjs.

List of all available formats (HH:mm:ss by default)

FormatOutputDescription
Yo2018thOrdinal formatted year
YY18Two-digit year
YYYY2018Four-digit year
M1-12The month, beginning at 1
Mo1st, 2nd, ..., 12thThe month, ordinal formatted
MM01-12The month, 2-digits
MMMJan-DecThe abbreviated month name
MMMMJanuary-DecemberThe full month name
D1-31The day of the month
Do1st, 2nd, ..., 31stThe day of the month, ordinal formatted
DD01-31The day of the month, 2-digits
H0-23The hour
Ho0th, 1st, 2nd, ..., 23rdThe hour, ordinal formatted
HH00-23The hour, 2-digits
h1-12The hour, 12-hour clock
ho1st, 2nd, ..., 12thThe hour, 12-hour clock, sorted
hh01-12The hour, 12-hour clock, 2-digits
m0-59The minute
mo0th, 1st, ..., 59thThe minute, ordinal formatted
mm00-59The minute, 2-digits
s0-59The second
so0th, 1st, ..., 59thThe second, ordinal formatted
ss00-59The second, 2-digits
SSS000-999The millisecond, 3-digits
AAM PMThe meridiem
AAA.M. P.M.The meridiem, periods
aam pmThe meridiem, lowercase
aaa.m. p.m.The meridiem, lowercase and periods
d0-6The day of the week, with Sunday as 0
ddS-SThe min name of the day of the week
dddSun-SatThe short name of the day of the week
ddddSunday-SaturdayThe name of the day of the week
zGMT, GMT+1The timezone with offset
zzGMT, GMT+1The timezone with offset
zzzGMT, GMT+1The timezone with offset
zzzzGMT, GMT+01:00The long timezone with offset
  • Meridiem is customizable by defining customMeridiem in options.

Demo

Monday 2025-03-10 03:47:06

Formatter Editor

Usage

Basic

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>

Use with 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>

Use with custom meridiem

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>

Type Declarations

Show Type Declarations
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://developer.mozilla.org/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.dev.org.tw/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>

Source

原始碼Demo文件

貢獻者

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 於 2025/3/5
7432f - feat(types): 棄用 MaybeRefMaybeRefOrGetter,改用 Vue 原生寫法 (#4636)
v12.6.0 於 2025/2/14
cd6d7 - feat: 新增 z...zzzz 以顯示時區資訊 (#4553)
v12.3.0 於 2025/1/2
59f75 - feat(toValue): 棄用 @vueuse/shared 中的 toValue,改用 Vue 原生寫法
v12.0.0-beta.1 於 2024/11/21
0a9ed - feat!: 移除 Vue 2 支援,最佳化 bundle 並清理程式碼 (#4349)
v11.0.0-beta.2 於 2024/7/17
4a7a8 - feat: locales 現在具有響應性 (#3907)
v10.6.0 於 2023/11/9
61ceb - feat: 新增日期序數格式化 (#3474)
v10.3.0 於 2023/7/30
d6428 - fix: 正確處理零值 (#3272)
v10.1.0 於 2023/4/22
a2147 - fix: 修正提供 Y 或 YYY 時發生的錯誤 (#3001)
v10.0.0-beta.4 於 2023/4/13
4d757 - feat(types)!: 將 MaybeComputedRef 重新命名為 MaybeRefOrGetter
0a72b - feat(toValue): 將 resolveUnref 重新命名為 toValue

在 MIT 許可證下發布。