Format Date with Intl.DateTimeFormat
Format dates for different locales and styles.
Code
JavaScript
const date = new Date("2024-03-07");
// Locale-aware
console.log(new Intl.DateTimeFormat("en-US").format(date));
// "3/7/2024"
console.log(new Intl.DateTimeFormat("en-GB", {
dateStyle: "full",
timeStyle: "short"
}).format(date));
// "Thursday, 7 March 2024 at 00:00"Line-by-line explanation
- 1.Intl.DateTimeFormat handles locale and options.
- 2.dateStyle: full | long | medium | short.
- 3.timeStyle for time portion.
- 4.format() returns the formatted string.
Expected output
3/7/2024