Skip to content

Date Formatting (useDateFormat)

useDateFormat is a Vue composable function for date formatting, and it also provides a non-reactive method formatDate.

It helps you easily handle common date formatting needs such as: timestamp parsing, cross-platform compatibility (e.g., iOS date parsing), formatting templates, and weekday display.

API Source Code: GitHub

Usage

vue
<template>
  <p>Current Time: {{ formatted }}</p>
  <button @click="date = '2025-12-31 23:59:59'">Modify Date</button>
</template>

<script setup>
import { ref } from "vue";
import { useDateFormat } from "see-u-ui";

const date = ref("2025-01-01 12:30:00");

// Reactive formatting (updates automatically when date changes)
const formatted = useDateFormat(date, "YYYY/MM/DD HH:mm:ss");
</script>
ts
import { formatDate } from "see-u-ui";

formatDate("2025-01-01 12:00:00", "YYYY-MM-DD");
// Returns: 2025-01-01

Formatting Rules

SyntaxMeaningExample
YYYY4-digit year2025
YY2-digit year25
MMMonth (zero-padded)01–12
MMonth (not zero-padded)1–12
DDDay (zero-padded)01–31
DDay (not zero-padded)1–31
HHHour (24h)00–23
hhHour (12h)01–12
mmMinute00–59
ssSecond00–59
S/SSSMillisecond (auto zero-padded)0–999 → 000
WWeek (short)Mon, Tue...
WWWeek (prefix)Mon, Tue...
WWWWeek (full name)Monday...

API

useDateFormat(date, formatStr?, options?)

Reactive Date Formatting

ParameterTypeDefaultDescription
date`MaybeRef<stringnumberDate>`
formatStrMaybeRef<string>"YYYY-MM-DD HH:mm:ss"Formatting template
optionsDateFormatOptions{ placeholder: '' }Other configuration options
options.placeholderstring''Content to display when date is invalid

Returns: ComputedRef<string>


formatDate(date, formatStr?, options?)

Non-reactive Formatting Utility Function

ParameterTypeDefaultDescription
date`stringnumberDate`
formatStrstring"YYYY-MM-DD HH:mm:ss"Formatting template
optionsDateFormatOptions{ placeholder: '' }Invalid date handling

Returns: string

Liao ICP No. 2025070134