9 Javascript Time And Date Manipulation Libraries For 2018

June 12, 2018 0 Comments

9 Javascript Time And Date Manipulation Libraries For 2018

 

 

Some day 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.

Wether 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 help you manipulate time. Individual modules from most of these libraries can be used with Bit, a platform that turns components and modules into building blocks which can be discovered, used, developed and synced from any project. Try it out.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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


Tag cloud