R
fn

useDateFormat

v0.0.15testeddemo

Reactively format a `Date`, timestamp, or date string against a token string (`YYYY MM DD HH mm ss SSS dddd A` etc.). Recomputes when the date, format, or locale changes.

Examples

ts
const formatted = useDateFormat(useNow(), 'YYYY-MM-DD HH:mm:ss');
ts
// Localized weekday + month, reactive locale
const locale = ref('fr-FR');
const label = useDateFormat(date, 'dddd, MMMM D', { locales: locale });
ts
// Custom meridiem
const t = useDateFormat(date, 'hh:mm a', {
  customMeridiem: (h) => (h < 12 ? 'morning' : 'evening'),
});

Demo

Loading demo…

Signature

ts
export function useDateFormat(
  date: MaybeRefOrGetter<DateLike>,
  formatStr: MaybeRefOrGetter<string> = 'HH:mm:ss',
  options: UseDateFormatOptions ={ ... }

Parameters

ParameterTypeDescription
dateMaybeRefOrGetter<DateLike>The date to format
formatStr?MaybeRefOrGetter<string>The token string
options?UseDateFormatOptionsLocale and meridiem options

Returns

UseDateFormatReturnThe reactive formatted string