JavaScript has a built-in Date
object that you can use to work with date and time in JavaScript. In fact, we have several tutorials on Envato Tuts+ that cover this topic in great detail.
THE Date
object in JavaScript has a few methods you can use to implement basic functionality. However, many of these methods work in your local time zone. You may also want better parsing capabilities for date strings or need to work with immutable dates.
In this tutorial, I’ll give you a quick overview of some of the best and most popular free date and time libraries in JavaScript.
Moment.js
Moment.js is one of the most used libraries for working with date and time in JavaScript. Its huge feature list makes it very attractive to developers. You can use the library to parse, manipulate and format dates and times.
Some of the features of Moment.js are:
- a solid and mature code base that provides many useful functions
- easy date parsing and formatting
- easy use of relative time (like 10 hours ago) or calendar time (like Sunday 6:00pm or yesterday 3:30pm)
- built-in support for multiple locales
One drawback of the library is its relatively large size, which can affect page load times. This is especially true if you will only be using a few features from the library.
Another point I should mention is that Moment.js is no longer being actively developed. It has now entered maintenance mode.
luxon
Luxon is a modern alternative to the Moment.js library. This library has some important advantages over Moment.js. It offers native timezone and internationalization support, which helps reduce file size. It’s less than a third the size of Moment.js at about 20kB compressed with gzip.
Some of the features of Luxon are:
- access to powerful features with a simple and performant API
- immutability and chainability of methods of writing concise code
- full support for localization and internationalization
- easy parsing and formatting of dates and times, ranges and durations
- great support for time zones and daylight savings to make things easier for developers
The library provides full support for ISO and Gregorian weekly calendars. It also provides limited support for other calendar systems such as Hebrew, Indian, Islamic and Japanese.
Day.js
Are you looking for a lightweight library to replace Moment.js? The Day.js library can be a great alternative in this case. One of the biggest advantages of the library is its small size of about 2 kB. The library works both in the browser and in Node.js.
Here are some useful library functions:
- an API that is almost fully compatible with Moment.js
- immutability support: any modification to the Day.js object will return a new instance instead of modifying the original one
- ability to manipulate time by adding or subtracting specified durations
- display the time relatively using the current time or another specific time
- parse any date and time string
You can extend the core functionality of the library with the use of plug-ins. There are many plug-ins available to add functionality as simple as calculating the day of the year. Other plugins will help you calculate the minimum and maximum of specified date values.
You can also create your own plugins for the library if none of the plugins offer what you need.
data-fns
The date-fns library is another great option for anyone looking for a modern, modular JavaScript date library. The library is very comprehensive in its feature coverage, while being easy to use at the same time.
What the lodash library does for arrays, the date-fns library does for dates. It has over 200 functions that can help you perform all kinds of tasks. Here are some of its interesting features:
- use the library to calculate the distance between two dates in words
- get the newest and oldest dates from an array
- support functions for different time periods, such as minutes helper that helps you get the minutes, go to the beginning or end of a minute, round up to the nearest minute, etc.
- weekday helpers will help you get the date for things like the following Thursday or the previous Tuesday
- additional packages such as data-fns-tz for timezone support
The common helper functions in the library can help you with date formatting and check if a date is in the past, present or future relative to other dates.
Using tree shaking will also help optimize the final packet size so you don’t load anything superfluous.
Chrono
The Chrono library is different from the others we’ve looked at in this article in that it doesn’t have a huge list of helper functions to help you manipulate dates. However, it has a unique purpose. The library is a natural language date parser. This means it can extract date and time information from any text and handle most date/time formats.
You can extract all this information from the given text just by calling the parse()
AND parseDate()
methods. You can also provide additional information and context for proper date parsing by using two other arguments with the parseDate()
method.
Consider the following example:
1 |
const parsedDate = chrono.parseDate("Meet me next Tuesday at 4:00 PM"); |
2 |
// Outputs: Date Tue Jun 13 2023 16:00:00 GMT+0530 (India Standard Time)
|
3 |
console.log(parsedDate); |
4 |
|
5 |
|
6 |
const parsedDate = chrono.parseDate("Meet me next Tuesday at 4:00 PM", new Date(2023, 5, 25)); |
7 |
// Outputs: Date Tue Jul 04 2023 16:00:00 GMT+0530 (India Standard Time)
|
8 |
console.log(parsedDate); |
In the first example, the parseDate()
The method examines the sentence and finds the date for me for next Tuesday from the current time. However, I provide context on the second call to find the next Tuesday after June 25, 2023. If so, it results in July 04, 2023.
The library even lets you define your own refiners to customize the results or add your own logic to Chrono.
Final thoughts
THE Date
object in JavaScript has a decent set of methods you can use to manipulate dates. However, you will need to rely on using the libraries if you work a lot with time zones or need to use a lot of helper functions such as those available with data-fns.
Similarly, libraries like Chrono can help you do something the native API simply can’t, like extract date/time data from text.
I hope you’ve found this list of popular, free, and open-source date-time libraries in JavaScript useful.