Course contentsShow
← Web Development
Lesson 2742 of 2,98249. Dates, Times, and Numbers on the WebFree lesson

Luxon: Formatting and Localization

Using toFormat, toLocaleString, and setLocale for human-readable, locale-aware date strings.

Luxon: Formatting and Localization

You'll learn how to transform Luxon DateTime objects into human-readable strings tailored to different languages and cultures.

Plain-English Overview

Once you've created and manipulated DateTime objects, you need to display them to users. Luxon provides powerful formatting tools that respect locale differences—meaning a date can appear as "January 5, 2024" in the US but "5 janvier 2024" in France. Think of it like a translation service specifically for dates: the underlying moment in time stays the same, but the presentation adapts to your audience.

Core Formatting Methods

toFormat() gives you precise control with tokens like 'yyyy-MM-dd' or 'MMMM d, yyyy':

const dt = DateTime.now();
dt.toFormat('yyyy-MM-dd');  // "2024-01-05"
dt.toFormat('MMMM d, yyyy'); // "January 5, 2024"
dt.toFormat('HH:mm:ss');    // "14:23:07"

toLocaleString() uses built-in locale-aware formats without memorizing tokens:

dt.toLocaleString(DateTime.DATE_FULL);
// "January 5, 2024"

dt.toLocaleString(DateTime.DATETIME_MED);
// "Jan 5, 2024, 2:23 PM"

Setting the Locale

setLocale() switches the language/region context:

dt.setLocale('fr').toLocaleString(DateTime.DATE_FULL);
// "5 janvier 2024"

dt.setLocale('ja').toLocaleString(DateTime.DATE_FULL);
// "2024年1月5日"

dt.setLocale('de').toFormat('MMMM');
// "Januar"

The locale affects month names, weekday names, number formats, and date ordering—all the cultural conventions around time display.

Key Takeaway: Use toFormat() for precise token-based formatting, toLocaleString() for standardized locale-aware output, and setLocale() to adapt DateTime objects to different languages and regional conventions.