Skip to content
On this page

useDateFormat

Category
Last Changed
3 weeks 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
YY18Two-digit year
YYYY2018Four-digit year
M1-12The month, beginning at 1
MM01-12The month, 2-digits
D1-31The day of the month
DD01-31The day of the month, 2-digits
H0-23The hour
HH00-23The hour, 2-digits
h1-12The hour, 12-hour clock
hh01-12The hour, 12-hour clock, 2-digits
m0-59The minute
mm00-59The minute, 2-digits
s0-59The second
ss00-59The second, 2-digits
SSS000-999The millisecond, 3-digits
d0-6The day of the week, with Sunday as 0

Demo

2022-05-25 07:17:50

Formatter Editor :

Usage

<script setup lang="ts">

import { ref, computed } from 'vue-demi'
import { useNow, useDateFormat } from '@vueuse/core'

const formatted = useDateFormat(useNow(), 'YYYY-MM-DD HH:mm:ss')

</script>

<template>
  <div>{{ formatted }}</div>
</template>
<script setup lang="ts">

import { ref, computed } from 'vue-demi'
import { useNow, useDateFormat } from '@vueuse/core'

const formatted = useDateFormat(useNow(), 'YYYY-MM-DD HH:mm:ss')

</script>

<template>
  <div>{{ formatted }}</div>
</template>

Type Declarations

export declare type DateLike = Date | number | string | undefined
export declare const formatDate: (date: Date, formatStr: string) => string
export declare const normalizeDate: (date: DateLike) => Date
/**
 * Get the formatted date according to the string of tokens passed in.
 *
 * @see https://vueuse.org/useDateFormat
 * @param date
 * @param formatStr
 */
export declare function useDateFormat(
  date: MaybeRef<DateLike>,
  formatStr?: MaybeRef<string>
): ComputedRef<string>
export declare type UseDateFormatReturn = ReturnType<typeof useDateFormat>
export declare type DateLike = Date | number | string | undefined
export declare const formatDate: (date: Date, formatStr: string) => string
export declare const normalizeDate: (date: DateLike) => Date
/**
 * Get the formatted date according to the string of tokens passed in.
 *
 * @see https://vueuse.org/useDateFormat
 * @param date
 * @param formatStr
 */
export declare function useDateFormat(
  date: MaybeRef<DateLike>,
  formatStr?: MaybeRef<string>
): ComputedRef<string>
export declare type UseDateFormatReturn = ReturnType<typeof useDateFormat>

Source

SourceDemoDocs

Contributors

Anthony Fu
Black
webfansplz

Changelog

v8.2.6 on 4/14/2022
a9c80 - feat: add format (#1491)
v8.0.0-beta.1 on 3/5/2022
5c445 - feat: new function (#1123)
useDateFormat has loaded