BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Luxon - Better Date and Time Management in JavaScript

Luxon - Better Date and Time Management in JavaScript

Luxon is a modern lightweight library for parsing, validating, manipulating, and formatting dates in JavaScript.

The JavaScript Date object is notoriously hard to use, and JavaScript developers often use 3rd party libraries such as Moment.js in order to get a broader, more convenient solution for date management.

Over the years Moment.js became the de-facto solution for JavaScript date management. However, at the end of 2020, the developers behind Moment.js decided to stop active development - stating that the code base was outdated, and refactoring the library to modern standards would require a complete rewrite.

Luxon, created by Isaac Cambron, a long-time contributor to Moment, offers a modern take on date management that addresses many of the shortcomings which exist in Moment.js, while offering a leaner and more concise solution that covers most common use cases; while Luxon is not a drop-in replacement for Moment.js, it is quickly becoming the preferred solution among JavaScript developers these days.

The most important class exposed by Luxon is the DateTime, which enables developers to parse a date object (or string), format it, and run basic math operations (i.e. add three days to the date).

For example:

let currentDate = DateTime.now();
let newDate = currentDate.plus({hours: 5});
let thirdDate = currentDate.setMonth(4);
console.log(currentDate.toLocaleString(DateTime.DATE_SHORT)

The first thing that stands out in this example is that the DateTime object is immutable. Therefore, any action that modifies the date will result in a new object.

It might come as a surprise to some Moment.js users, but immutability was a highly requested feature, and while it might feel counter-intuitive at first, it prevents many common mistakes.

In addition to the DateTime class, Luxon provides another useful class called Duration.

As its name suggests, Duration represents a period of time, i.e., "three months". Just like the DateTime object, it can be manipulated and parsed, for example:

let dur = Duration.fromISOTime('11:22');
console.log(dur.toHuman()); // '0 day, 11 hours, 22 minutes'

Developers can find a complete list of available commands for both the DateTime and Duration classes in the official API.

It's worth noting that work is underway to add a new date time API called Temporal API to JavaScript, which addresses many existing problems with the Date object.

The API is currently in stage three in the TC39 committee process, which means significant browser support should be coming relatively quickly.

Luxon is open-source software available under the MIT license. Contributions and feedback are encouraged via the GitHub project and should follow the Luxon contribution guidelines.

About the Author

Rate this Article

Adoption
Style

BT