Top 6 JavaScript Date libraries in 2019

Adding working dates to your application appears to be a simple task theoretically until you try to do it. There is always something getting in the way, regardless of whether it’s a time zone issue when trying to internationalize your website, issues showing the date in the needed format, or even trying to do some basic arithmetic with them (i.e subtracting three days from a date, it shouldn’t be that difficult, should it?).

The issue is that, sadly, JavaScript isn’t generally prepared to locally manage dates. Which is somewhat unexpected thinking of it does have a Date object we can use for some basic operations.

What does it mean when we say JavaScript isn’t ready? This implies that the API for this object isn’t rich enough for our day by day needs, it doesn’t have the high level support you’d expect from such an object to take on issues, for example, internationalization, timezone support, and so on. Here comes the libraries in focus. Date libraries add layers of abstraction on top of JavaScripts Date Object and make it useful. Let us see Some best Javascript libraries.

Top 6 JavaScript Date Libraries-

1. moment.js-

Moment.js is one of the oldest and best known date-related libraries for both, Node.js and vanilla JavaScript (making it browser compatible). Because of this, they’ve had the time to make it compatible with huge numbers of the most widely recognized web- development utilities, for example,

  • Bower
  • requireJS
  • Nuget
  • Browsify
  • TypeScript
  • meteor

It offers a great API without having to modify the Date object’s original prototype, rather, they created a wrapper object around it, ensuring they don’t disturb the original namespace.

As an example, here is how you’d set up the current date. Add seven days, then subtract a month, and finally set the year and time values. All in a single line of easy-to-read code, like this:

moment().add(7, 'days').subtract(1,'months').year(2009).hours(0).minutes(0).seconds(0);

They support over 20 different locales, so if you’re focusing to solve your internationalization problems, moment.js is a good.

2. date-fns-

Date-fns is a growingly prominent time manipulation library for Javascript, chosen by numerous developers to replace moment.js. This library gives more than 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 functions well with bundlers, for example, webpack, Browserify, or Rollup and also supports tree-shaking. It can also be joined with Bit to make each function exclusively available to install, use and even modify.

3. Luxon-

Luxon is built by one of Moment’s maintainers. It is 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 implies creating new instance with various properties.

4. DayJS-

Having 12K stars day.js is a 2KB date library option 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. Rather than modifying the native Date.prototype, Day.js makes a wrapper for the Date object, called Dayjs object. All API operations that change the Dayjs object in some way will return new instance of it.

5. ms-

To change things up a bit, this next library was not created as a substitution for MomentJS yet rather, it just has one job, and it does so very well. The goal of MS is to change any kind of date format into milliseconds and back. This is a restricted use case, but as you know, turning dates into milliseconds has its advantages, particularly in case you’re attempting to do things like comparison, and a few types of arithmetic operations (it’s significantly simpler to add 1000 milliseconds to a number, than saying you have to add 1 second to a Date object).

So in other words, with this library you can do things like this:

ms('2 days')  // 172800000
ms('1d')      // 86400000
ms('10h')     // 36000000
ms('2.5 hrs') // 9000000
ms('2h')      // 7200000
ms('1m')      // 60000
ms('5s')      // 5000

ms(60000)             // "1m"
ms(2 * 60000)         // "2m"
ms(-3 * 60000)        // "-3m"

This library has over 30 million downloads every week. It covers a very specific and yet, common use case. 

6. Spacetime-

This library has a very interesting focused: timezones. It provides a MomentJS-like API, with immutable objects also. But the main focus of this library is to help you to manage timezones more easily. Also, remember, timezones will in general be one of the primary issues when managing any type of internationalization development, so this looks promising.

Let us see some code:

const spacetime = require('spacetime')

let d = spacetime('March 1 2012', 'America/New_York')
//set the time
d = d.time('4:20pm')
console.log(d.time())

d = d.goto('America/Los_Angeles')
console.log(d.time())
//'1:20pm'

That was very simple. A note about Spacetime however, is that it doesn’t depend on the Intl API like Luxon did, so the library itself isn’t generally lightweight sitting at around 40Kb. That being stated, timezones still follow the IANA naming convention, which is great for standardizing your code.

Another cool thing about library is that it can observe daylight saving times where applicable, so the following happens:

d = d.goto('Eastern Time') // "America/New_York"
d = d.goto('PST') // automatically becomes 'PDT' in the summer

There are other clever techniques, for example, season which returns the current season for a specific date (either summer/winter/spring/fall is returned) as well as hasDST and inDST which return whether a timezone ever uses daylight saving times and if it has it active at the specific date/time setup.

Finally, extending Spacetime is quite simple, which helps add extra features, such as:

spacetime.extend({
  isHappyHour: function() {
    return this.hour() === 16
  }
})
d = d.time('4:15pm')
console.log(d.isHappyHour())
//true

You can also refer Top 3 JavaScript frameworks and 10 JavaScript concepts every Node.js programmer must master.

Conclusion-

These are common JavaScript libraries that can deal date and time. If you’re having to deal with very complex date-time related features, go with either MomentJS because it’s a classic and time tested library, or simply try the DayJS.

If you’re still confused for selecting best JavaScript date library, consult with solace experts. Solace team is well proficient to give you the best solution of JavaScript libraries as per your requirement. We will be more happy to help you.

Related Post