9 Javascript Time and Date Manipulation Libraries for 2019

9 Javascript time and date manipulation libraries to use in 2019

Jonathan Saring
Bits and Pieces

--

Someday we might create a library for actual time travel. Until then, we’ll have to make do with libraries that manipulate date and time in our apps.

Whether you need to create a calendar, an appointment scheduling system or an interface to integrate different people at different time zones, manipulating time in Javascript is essential the most Javascript applications. Here are 9 useful and actively maintained libraries to choose from.

Tip: Use Bit to choose and use individual functions from these libraries. Each can be individually discovered, played-with and installed without the library.

Here is an example with date-fns:

date-fns functions with Bit: choose, play, use

1. Date-fns

Date-fns is a growingly popular (11K stars, over half a million weekly NPM installs) time manipulation library for Javascript, chosen by many developers to replace moment.js (see comparison). Date-fns provides over 130 functions to manipulate dates in the browser and Node.js. Date-fns is built using pure functions and keeps things immutable while not changing passed date instances. It works well with bundlers such as webpack, Browserify, or Rollup and also supports tree-shaking. It can also be combined with Bit to make every function individually available to install, use and even modify.

The library also supports both Flow andTypeScript.

2. Moment

Moment.js is a well-known, useful and somewhat heavy time manipulation library designed to work both in the browser and in Node.js. At nearly 40K stars, Moment helps you parse, validate, manipulate, and display dates in javascript. Note that the moment object itself is mutable, so different operations change the original moment object. Here’s a useful cheatsheet.

3. Luxon

Built by one of Moment’s maintainers, and at 6.5K stars, luxon is a a Javascript library for working with dates and times with a more comprehensive interface than the native types it wraps. Unlike moment, Luxon objects are immutable so making changes actually means creating a new instance with different properties. Here’s a quick guide to creating your first DateTime class with Luxon.

4. Day JS

Credit: http://nmotw.in/dayjs/

At nearly 12K stars day.js is a 2KB date library alternative to Moment.js with a similar API. This library also helps you parse, validate, manipulate, and display dates and times, and is also immutable and chainable. Instead of modifying the native Date.prototype, Day.js creates a wrapper for the Date object, called Dayjs object. All API operations that change the Dayjs object in some way will return a new instance of it.

5. Ms.js

Brought to us by Zeit, ms.js is a narrow yet surprisingly useful library that converts various time formats into milliseconds. It also works the other way around, converting milliseconds into human-readable time formats. ms works in both Node.js and the browser.

6. Timeago.js

Timeago is a ~2KB library that performs a specific useful action: converting time formats to present how much time has passed since then. 0 dependencies, and the library also supports localization. Here’s a demo.

7. Instadate

At “only” 800 stars, Instadate is a small library with a handful of performance orianted features. Instadate is more of a wrapper around the native JavaScript Date than a full on date library. It can run thousands of time and date manipulations per second, and also works with ES5 and ES6. The library has a handful of dependancies, including lodash.difference.

8. JS-Joda

At “only” 900 stars, JS-Joda is an immutable date and time library for javascript with a domain-driven API based on the ISO8601 calendar. It’s uniqueness lies in the fact that it doesn’t wrap Javascript’s Date object. Instead, , js-joda is a standalone date and time implementation. The library has no external dependancies. Here’s a link to the quick-start guide.

9. Spacetime

Spacetime is a lightweight javascript timezone library which, given its size, performs some pretty complex calculations and manipulations for working with timezones even taking into account daylight savings, hemispheres, leap-years and more, all with an API similar to Moment and no external dependencies.

Honorable mentions

Note that these libraries are mostly either old and unmaintained, or very young, so dive in with care.

--

--

I write code and words · Component-driven Software · Micro Frontends · Design Systems · Pizza 🍕 Building open source @ bit.dev