Skip to content

Instantly share code, notes, and snippets.

@roshanca
Created July 18, 2022 02:50
Show Gist options
  • Save roshanca/08e462e197a46cec7322388985065163 to your computer and use it in GitHub Desktop.
Save roshanca/08e462e197a46cec7322388985065163 to your computer and use it in GitHub Desktop.
interface Options {
year?: "numeric" | "2-digit"
month?: "long" | "short"
day?: "numeric"
}
export default function formatDate(date: Date | string = ""): string {
const options: Options = {
year: "numeric",
month: "long",
day: "numeric",
}
if (date instanceof Date) {
return date.toLocaleDateString("en-US", options)
} else {
return new Date(date).toLocaleDateString("en-US", options)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment