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
dateTime.toFormat(format)
dateTime: a Luxon date, such as one made withDateTime.fromISO().format: a string of tokens. Each token becomes part of the date; other characters, such as-and,, stay as they are.
| Token | Gives | Example |
|---|---|---|
yyyy | the year | 2026 |
MM | the month as two digits | 11 |
LLL | the month’s short name | Nov |
dd | the day as two digits | 27 |
HH | the hour, 00 to 23 | 14 |
mm | the minute as two digits | 05 |
See Formatting (opens in a new tab) in the Luxon documentation for every token.
Prerequisites
- Node.js, which comes with npm. Install it from the Node.js download page (opens in a new tab).
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:
npm install luxon
added 1 package in 276ms
Step 2: Write the Code
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
node date.js
2026-11-27
27 Nov 2026, 14:05
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Small letters for the month: yyyy-mm-dd | mm is the minute, so the example gives 2026-05-27 | Write 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
- Formatting (opens in a new tab) (Luxon docs)
- Install guide (opens in a new tab) (Luxon docs)
- Run Node.js scripts from the command line (opens in a new tab) (Node.js docs)
JavaScript is a trademark of Oracle Corporation in the United States. Node.js is a trademark of the OpenJS Foundation.
0 comments