Working with dates in JavaScript isn’t easy. That’s why if you look at the package.json files of most apps, you’ll usually find a library like Moment.js in there.
Moment.js was one of the first libraries to gain notoriety. It made parsing/formatting/calculating dates less daunting for developers.
But do you know about a library called date-fns?
date-fns is generally considered to be a worthy alternative to Moment.js. Not only because it offers the same feature set, but it also appeals to functional programmers.
You can install date-fns with npm/Yarn:
Formatting dates is the bread and butter of libraries like Moment.js/date-fns. This is because native JavaScript doesn’t have a simple way to handle this.
date-fns uses string patterns similar to Moment.js:
It’s that simple! There are lots of ways to format dates so they look exactly the way you want them to.
Now that we can format dates, what about performing addition/subtractions with them? There are several functions for doing this:
In the example below, we add 1 year to a specified date:
Formatting is pretty easy, but what about locales? We know that users will be visiting your website from around the world, and we don’t want to assume they speak our native language.
For this, we can import specific locale plugins:
The ability to calculate the differences between 2 dates is important for a date manipulation library. date-fns provides several functions for calculating this.
For example, we can calculate the days from January 1st to Christmas (as well as “business days”!):
One of the biggest downsides to using Moment.js is that it’s large! There isn’t a way to import individual functions because its API only allows for chaining. This means you’re required to import the entire library:
With date-fns, you only grab the specific functions you need (a lot like with Lodash):
This makes date-fns a much smaller dependency than Moment.js. See the graphic below for the bundle sizes of Moment.js vs date-fns:
Source: BundlePhobia
Note that there’s a way to configure webpack to exclude the “locale” plugins. This would significantly reduce its bundle size.
date-fns seems to be getting a lot more development work than Moment.js at the moment 😉. For this reason, it’s really well-maintained, and developers get more input into its direction. Along with its robust feature set and modern ES6 sensibilities, it’s worth giving it a try! 📅📌
The official docs for date-fns are really great, and have lots of code samples!
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!