useDateFormat

useDateFormat

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

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

フォーマット出力説明
Yo2018th序数形式の年
YY182桁の年
YYYY20184桁の年
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オフセット付きの長いタイムゾーン
  • 午前午後は、optionscustomMeridiem を定義することでカスタマイズ可能です。

使用法

基本

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

ロケールと一緒に使用

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

カスタム午前午後を使用

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 Μ.Μ.'