Skip to content

Instantly share code, notes, and snippets.

@nathanlogan
Created January 27, 2022 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathanlogan/828abceb440b9533c2a8e07e5e95bd7f to your computer and use it in GitHub Desktop.
Save nathanlogan/828abceb440b9533c2a8e07e5e95bd7f to your computer and use it in GitHub Desktop.
Convert date string to user's locale
export const formatDateAsDefaultLocale = (incomingDate: string | Date, options?: Intl.DateTimeFormatOptions): string => {
const defaultOptions = { day: 'numeric', month: 'short', year: 'numeric' }
const date = new Date(incomingDate)
return date.toString() !== "Invalid Date" ? date.toLocaleString('default', options || defaultOptions) : '';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment