How to localize date format in TypeScript

1 Answer

0 votes
// Localized date formatting in TypeScript using Intl.DateTimeFormat

const now = new Date();

// 1. Localized date using the system locale
console.log("--- Using system locale ---");

console.log(
  "Short date :",
  new Intl.DateTimeFormat(undefined, {
    year: "numeric",
    month: "numeric",
    day: "numeric"
  }).format(now)
);

console.log(
  "Long date  :",
  new Intl.DateTimeFormat(undefined, {
    weekday: "long",
    year: "numeric",
    month: "long",
    day: "numeric"
  }).format(now)
);

console.log(
  "Time       :",
  new Intl.DateTimeFormat(undefined, {
    hour: "2-digit",
    minute: "2-digit",
    second: "2-digit"
  }).format(now)
);

console.log(
  "Weekday    :",
  new Intl.DateTimeFormat(undefined, {
    weekday: "long"
  }).format(now)
);

console.log(
  "Month name :",
  new Intl.DateTimeFormat(undefined, {
    month: "long"
  }).format(now)
);


// 2. Localized date using a specific locale (example: French)
const locale = "fr-FR";

console.log("\n--- Using French locale ---");

console.log(
  "Short date :",
  new Intl.DateTimeFormat(locale, {
    year: "numeric",
    month: "numeric",
    day: "numeric"
  }).format(now)
);

console.log(
  "Long date  :",
  new Intl.DateTimeFormat(locale, {
    weekday: "long",
    year: "numeric",
    month: "long",
    day: "numeric"
  }).format(now)
);

console.log(
  "Weekday    :",
  new Intl.DateTimeFormat(locale, {
    weekday: "long"
  }).format(now)
);

console.log(
  "Month name :",
  new Intl.DateTimeFormat(locale, {
    month: "long"
  }).format(now)
);


// 3. Custom localized format (weekday + day + month + year + time)
console.log("\n--- Custom French format ---");

console.log(
  new Intl.DateTimeFormat(locale, {
    weekday: "short",
    day: "numeric",
    month: "long",
    year: "numeric",
    hour: "2-digit",
    minute: "2-digit"
  }).format(now)
);



/*
run:

--- Using system locale ---
Short date : 4/19/2026
Long date  : Sunday, April 19, 2026
Time       : 05:04:41 PM
Weekday    : Sunday
Month name : April

--- Using French locale ---
Short date : 19/04/2026
Long date  : dimanche 19 avril 2026
Weekday    : dimanche
Month name : avril

--- Custom French format ---
dim. 19 avril 2026 à 17:04

*/

 



answered 6 hours ago by avibootz
...