diff --git a/packages/shared/useDateFormat/index.ts b/packages/shared/useDateFormat/index.ts index 931dac47543..c9bf38deb51 100644 --- a/packages/shared/useDateFormat/index.ts +++ b/packages/shared/useDateFormat/index.ts @@ -29,7 +29,15 @@ const defaultMeridiem = (hours: number, minutes: number, isLowercase?: boolean, return isLowercase ? m.toLowerCase() : m } -export const formatDate = (date: Date, formatStr: string, options: UseDateFormatOptions) => { +const isLocaleArgument = ( + arg: UseDateFormatOptions | Intl.LocalesArgument, +): arg is Intl.LocalesArgument => typeof arg === 'string' || arg instanceof Intl.Locale || Array.isArray(arg) + +export const formatDate = (date: Date, formatStr: string, options: UseDateFormatOptions | Intl.LocalesArgument) => { + // handle deprecated `locales` argument + if (isLocaleArgument(options)) + options = { locales: options } + const years = date.getFullYear() const month = date.getMonth() const days = date.getDate() @@ -44,8 +52,8 @@ export const formatDate = (date: Date, formatStr: string, options: UseDateFormat YYYY: () => years, M: () => month + 1, MM: () => `${month + 1}`.padStart(2, '0'), - MMM: () => date.toLocaleDateString(options.locales, { month: 'short' }), - MMMM: () => date.toLocaleDateString(options.locales, { month: 'long' }), + MMM: () => date.toLocaleDateString((options).locales, { month: 'short' }), + MMMM: () => date.toLocaleDateString((options).locales, { month: 'long' }), D: () => String(days), DD: () => `${days}`.padStart(2, '0'), H: () => String(hours), @@ -58,9 +66,9 @@ export const formatDate = (date: Date, formatStr: string, options: UseDateFormat ss: () => `${seconds}`.padStart(2, '0'), SSS: () => `${milliseconds}`.padStart(3, '0'), d: () => day, - dd: () => date.toLocaleDateString(options.locales, { weekday: 'narrow' }), - ddd: () => date.toLocaleDateString(options.locales, { weekday: 'short' }), - dddd: () => date.toLocaleDateString(options.locales, { weekday: 'long' }), + dd: () => date.toLocaleDateString((options).locales, { weekday: 'narrow' }), + ddd: () => date.toLocaleDateString((options).locales, { weekday: 'short' }), + dddd: () => date.toLocaleDateString((options).locales, { weekday: 'long' }), A: () => meridiem(hours, minutes), AA: () => meridiem(hours, minutes, false, true), a: () => meridiem(hours, minutes, true),