Handle Date and Time Operations With Luxon, a Modern JavaScript Date Library

You can use JavaScript to build dynamic web applications and applications that run in other environments, including the desktop. Many apps need to handle dates and times, from schedulers and chat applications to event booking apps.

Luxon offers a superior alternative to the native JavaScriptDateobject, with more user-friendly, reliable ways to handle date and time operations.

4

Installing Luxon

You can add Luxon to your JavaScript app in several ways, depending on your preferred method. The two most common methods are to usenpmto install the library or to use a CDN link to include the library in your application.

To install Luxon in a Node.js project, run the following npm command in your terminal:

A collage showing stills from The Walking Dead, The Wire, House of Cards, Line of Duty, Manifest, and the Man in the High Castle

Use the library in your JavaScript file by requiring luxon:

This code usesobject destructuringto import theDateTimeclass from the Luxon library and create a new variableDateTimereferencing that class.

Feed view open on a Notion database

This allows you to create instances of dates and times and perform operations such as formatting dates for display.

Alternatively, you can include Luxon in your application using aCDN link. To do this, add the following markup to your HTML file:

Spotify Daylist on an iPad with AirPods nearby

Using this approach, you can run scripts usingLuxonin the body of your HTML.

Features of Luxon

Luxon provides many features, making it a valuable package for handling dates and times in JavaScript applications.

Comprehensive Date and Time Manipulation

Luxon offers various methods for creating, manipulating, and retrieving dates and times. Luxon simplifies tasks such as adding or subtracting durations, setting specific date/time components, and performing relative time calculations.

Formatting and Localization

Luxon’s formatting options allow you to display dates and times in many formats. Luxon supports different format patterns and lets you set dates and times according to other languages.

Timezone Support

With Luxon, you can efficiently work with dates and times in different time zones. Luxon provides methods to set and convert between time zones.

Immutable Design

Luxon follows an immutable design pattern, ensuring thatDateTimeobjects are immutable. This design approach provides predictable and consistent behavior when working with dates and times.

Extensive API Documentation

Luxon’s API documentation is very detailed, making it easy to explore and understand the available methods, options, and functionality.

The DateTime Class in Luxon

TheDateTimeclass in Luxon represents a particular date and time. It offers both class and instance methods that let you carry out various tasks. These tasks include creating, parsing, retrieving, modifying, and formatting dates and times.

Here are some of the different ways to create a new instance of the Date Time class in Luxon:

A laptop sitting on a counter in front of a window. The laptop has some JavaScript code on the screen.

Formatting DateTime Objects to Strings

Luxon simplifies the formatting ofDateTimeobjects into strings representing specific dates and times. You can use Luxon to format dates and times in various methods.

The ISO 8601 format is widely used for standardized date and time representation. To format aDateTimeobject as an ISO 8601 string, use thetoISO()method:

Human Readable Formats

Luxon supports human-readable formats that you may customize for local languages. You can format aDateTimeobject to a human-readable string with thetoLocaleString()method:

Token Based Formatting

Token-based formatting allows you to format date and time into custom strings using placeholders called tokens. To format aDateTimeobject using tokens, use thetoFormat()method:

In the example above, the format stringyyyy-MM-dd HH:mm:ssrepresents the desired output format. The tokensyyyy,MM,dd,HH,mm, andsscorrespond to the year, month, day, hour, minute, and second of theDateTimeobject, respectively.

Luxon allows you to perform a wide range of date and time representations by providing an extensive set of tokens.

Parsing and Validating Dates and Times in Luxon

Luxon provides robust methods for parsing and validating dates and times. These features are useful for tasks such as validating user input or converting string representations of dates and times intoDateTimeobjects.

The fromFormat() Method

ThefromFormat()method allows you to parse a string representation of a date and time, and converts it into aDateTimeobject. It takes two arguments, the input string and a format string specifying the input format.

For example:

Validating Dates and Times With the isValid Method

TheisValidmethod checks if aDateTimeobject represents a valid date and time. It returnstrueif the object is valid andfalseotherwise.

In the example, the firstDateTimeobject represents an invalid date because February 29, 2022, is not valid. The secondDateTimeobject represents a valid date of February 28, 2022.

Parsing and validation features in Luxon help you accurately handle dates and times, validate inputs, and provide reliable representations of date and time data.

Alternatives to Luxon

You have learned how to create, format, and validate date and time instances with Luxon. These tips will assist you when building web apps that require date and time handling.

You can also utilize other JavaScript packages to achieve date and time handling in your web applications, including Day.js and Date.fns. These packages have their advantages and disadvantages, and you should base your preference on your specific needs.

Go beyond JavaScript’s built-in date and time capabilities with one of these replacement libraries.

It’s not super flashy, but it can help to keep your computer up and running.

I plugged random USB devices into my phone and was pleasantly surprised by how many actually worked.

It saves me hours and keeps my sanity intact.

My iPhone does it all, but I still need my dumb phone.

Some subscriptions are worth the recurring cost, but not these ones.

Technology Explained

PC & Mobile