Skip to content

Commit

Permalink
fix: Accept any type of date in the f function
Browse files Browse the repository at this point in the history
`date-fns` functions do not accept dates formatted as strings anymore, but only date objects.
We make it so the `f` function returned by the `useI18n` hook still accepts other types of values, strings in particular.
This should help consuming libs and apps to migrate.
  • Loading branch information
PolariTOON committed Dec 19, 2022
1 parent 0cf8966 commit f83bcff
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions react/I18n/format.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,17 @@ export const initFormat = (userLang, defaultLang = DEFAULT_LANG) => (
date,
formatStr
) => {
const dateObject = new Date(date)
const locale = provideDateFnsLocale(userLang, defaultLang)
return format(date, formatStr, { locale })
return format(dateObject, formatStr, { locale })
}

export const formatLocallyDistanceToNow = date =>
formatDistanceToNow(date, { locale: locales[lang] })
export const formatLocallyDistanceToNow = date => {
const dateObject = new Date(date)
return formatDistanceToNow(dateObject, { locale: locales[lang] })
}

export const formatLocallyDistanceToNowStrict = date =>
formatDistanceToNowStrict(date, { locale: locales[lang] })
export const formatLocallyDistanceToNowStrict = date => {
const dateObject = new Date(date)
return formatDistanceToNowStrict(dateObject, { locale: locales[lang] })
}

0 comments on commit f83bcff

Please sign in to comment.