コンテンツへスキップ

useDateFormat

カテゴリ
エクスポートサイズ
807 B
最終更新日
先週

dayjsに着想を得て、渡されたトークンの文字列に従ってフォーマットされた日付を取得します。

使用可能なすべてのフォーマットのリスト(デフォルトはHH:mm:ss)

フォーマット出力説明
Yo2018th序数形式の年
YY182桁の年
YYYY20184桁の年
M1-12月(1から始まる)
Mo1st, 2nd, ..., 12th序数形式の月
MM01-122桁の月
MMMJan-Dec月の略称
MMMMJanuary-December月の正式名称
D1-31月の曜日
Do1st, 2nd, ..., 31st序数形式の月の曜日
DD01-312桁の月の曜日
H0-23
Ho0th, 1st, 2nd, ..., 23rd序数形式の時
HH00-232桁の時
h1-1212時間制の時
ho1st, 2nd, ..., 12th12時間制の時(序数形式)
hh01-1212時間制の2桁の時
m0-59
mo0th, 1st, ..., 59th序数形式の分
mm00-592桁の分
s0-59
so0th, 1st, ..., 59th序数形式の秒
ss00-592桁の秒
SSS000-9993桁のミリ秒
AAM PM午前/午後
AAA.M. P.M.午前/午後(ピリオド付き)
aam pm午前/午後(小文字)
aaa.m. p.m.午前/午後(小文字、ピリオド付き)
d0-6曜日の数値(日曜日を0とする)
ddS-S曜日の略称
dddSun-Sat曜日の略称
ddddSunday-Saturday曜日の正式名称
  • 午前/午後は、`options`で`customMeridiem`を定義することでカスタマイズできます。

デモ

2024年11月28日(木) 03:59:12

フォーマッタエディタ

使用方法

基本

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 {
  /**
   * 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.dokyumento.jp/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>

ソース

ソースコードデモドキュメント

貢献者

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

変更ログ

v12.0.0-beta.1 2024年11月21日
0a9ed - feat!: Vue 2サポートの削除、バンドルの最適化、クリーンアップ (#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: 0を適切に処理 (#3272)
v10.1.0 2023年4月22日
a2147 - fix: YまたはYYYが指定された場合のエラー (#3001)
v10.0.0-beta.4 2023年4月13日
4d757 - feat(types)!: MaybeComputedRefMaybeRefOrGetterにリネーム
0a72b - feat(toValue): resolveUnreftoValueにリネーム
v9.3.0 2022年9月26日
3fda6 - feat: 子午線形式をサポート (#2011)
39b27 - feat: MMMとMMMMフォーマッタをサポート (#2234)

MITライセンスの下でリリースされています。