Skip to main content
0 reactions

How to Format a Date with Luxon toFormat() in JavaScript

Card titled Luxon toFormat(), labelled JavaScript basics, with four ticked lines: the format yyyy-MM-dd gives 2026-11-27, and dd LLL yyyy, HH:mm gives 27 Nov 2026, 14:05.

Luxon is a JavaScript library for dates and times. Its toFormat() turns a date into text in the layout you describe with tokens: yyyy-MM-dd gives 2026-11-27.

Every command and output on this page is real, run with Node.js 26.13.1, npm 12.2.3 and Luxon 3.7.2 on macOS.

Syntax

SyntaxJavaScript
dateTime.toFormat(format)
  • dateTime: a Luxon date, such as one made with DateTime.fromISO().
  • format: a string of tokens. Each token becomes part of the date; other characters, such as - and ,, stay as they are.
TokenGivesExample
yyyythe year2026
MMthe month as two digits11
LLLthe month’s short nameNov
ddthe day as two digits27
HHthe hour, 00 to 2314
mmthe minute as two digits05

See Formatting (opens in a new tab) in the Luxon documentation for every token.

Prerequisites

The Example

A meeting on 27 November 2026 at 14:05, written two ways, in a file named date.js in a new folder.

Step 1: Install Luxon

In Terminal, in the new folder, install Luxon from npm:

TerminalBash
npm install luxon
OutputPlain text
added 1 package in 276ms

Step 2: Write the Code

date.jsJavaScript
const { DateTime } = require('luxon');

const meeting = DateTime.fromISO('2026-11-27T14:05:00');

console.log(meeting.toFormat('yyyy-MM-dd'));
console.log(meeting.toFormat('dd LLL yyyy, HH:mm'));

require('luxon') loads Luxon, and DateTime.fromISO() makes a date from the text 2026-11-27T14:05:00.

Step 3: Run It

TerminalBash
node date.js
OutputPlain text
2026-11-27
27 Nov 2026, 14:05

Common Mistakes

MistakeWhat happensDo this instead
Small letters for the month: yyyy-mm-ddmm is the minute, so the example gives 2026-05-27Write the month as MM

FAQ

Can I format the current date?

Yes. DateTime.now() gives the current date and time, and DateTime.now().toFormat('yyyy-MM-dd') printed 2026-11-27 on the day of this example.

Conclusion

Describe the layout with tokens and toFormat() fills in the date. Watch the capitals: MM is the month and mm the minute.

Additional Resources

JavaScript is a trademark of Oracle Corporation in the United States. Node.js is a trademark of the OpenJS Foundation.

0 comments