Img source: christosfellas.com

One of the best JavaScript libraries for working with dates is moment. It is really easy to install, use and has many functions that help you with parsing, validating, manipulating and formatting dates.

You can install it using npm using the following command:

npm install moment

Its documentation demonstrates how you can also install and use it using other ways like Bower, NuGet, meteor and many others.

Some of use cases of moment can be seen down below:

  • Get current date:
    moment();
  • Create a moment from a string:
    var day = moment("2018-03-18");
  • Validate a date:
    moment().isValid();
  • Find a later date:
    invalid.add(unit, value)
  • Check if a date is after another date:
    date.isAfter(another);
  • Check if a date is same or after another date:
    moment('2018-03-20').isSameOrAfter('2018-03-19');
  • Check if a date is before another date:
    date.isBefore(another)
  • Check if a date is same or before another date:
    moment('2010-10-20').isSameOrBefore('2010-10-21');  
  • Check if a year is leap year
    moment().isLeapYear();

You can learn about it by reading its documentation. This library is open source and can be found on GitHub.