Format Date

Utility function to format date

Code

snippets/typescript/format-date.ts
export const formatDate = (date: Date) => {
  if (!date) {
    return "";
  }


  try {
    return new Intl.DateTimeFormat("en-US", {
      year: "numeric",
      month: "long",
      day: "numeric",
    }).format(date);
  } catch (e) {
    console.error(e);


    return "";
  }
};

Examples

snippets/typescript/format-date.example.ts
import { formatDate } from "./format-date";


formatDate(new Date("2021-01-01")); // January 1, 2021

Tests

snippets/typescript/format-date.test.ts
import { describe } from "bun:test";


describe.todo("formatDate", () => {});

Sources