Getting started with Moment.js: Intro with examples.

Reading Time: 2 minutes

I think date manipulation is most common process that every developer must deal in his career. There are so many tools available for Date Manipulation in JavaScript like datejs, date-fns and so many interesting library. Moment.js is one of them.

Developer can easily interact with date and time domain problems by using Moment.js. JavaScript Date requires lines of code for parsing , manipulation and validation.

Instead of modifying the native Date.prototype, Moment.js creates a wrapper for the Date object. To get this wrapper object, simply call moment() with one of the supported input types. Moment.js has a very flexible and advanced parser that allows a huge range of functionality.

Before begin, include the library from cdnjs in your HTML code.

To get current date and time, just call moment() with no arguments.

var currentTime = moment(); //same as calling moment(new Date());

Parsing and Validation of Dates

  • To get date so many another different formats.
var date = moment.format("MM-DD-YYYY"); // will return in MM-DD-YYYY format
var date2 = moment.format("DD/MM/YYYY"); // will return in DD/MM/YYYY format
  • To parse date time string.
moment("03-06-1995", "MM-DD-YYYY");

The parser ignores non-alphanumeric characters, so both of the following will return the same thing.

moment("03-06-1995", "MM-DD-YYYY");
moment("03/06/1995", "MM-DD-YYYY");
  • For validation for date, we can use isValid() function
moment.isValid(); //return true
var m = moment("2011-25-08T12:65:20");
m.isValid(); // return false
m.invalidAt(); // return 4 for minutes

The return value for invalidAt() has the following meaning :

0 for years, 1 for months, 2 for days, 3 for hours, 4 for minutes, 5 for seconds,6 for milliseconds.

Manipulation of Dates

Once you have a Moment, you can manipulate Date in some way. There are a number of methods to help with this. Moment.js uses method chaining.

You can add, subtract or setting to start or end of day, month and year. For example,

moment().add(360,'days'); // add 360 Days to current Date
moment().subtract(7,'months') // subtact 7 months from current Date
moment().startOf('year');// set to January 1st, 12:00 am this year
moment().endOf('year');// set the moment to 12-31 23:59:59.999 this year

As you all have seen Moment.js is a great library to have for woking with dates in JavaScript. We have covered just some basics in this article. It has lot of other cool features to offer. Check out their documentation and source code on GitHub.


Written by 

Aakash Jain is a software consultant having more than 1 year of experience. Aakash likes to explore new technologies. He loves online gaming, watching marvels movies, playing badminton and exploring new places. Aakash is familiar with programming languages such as Angular 4, Java, Scala, C++, TypeScript, HTML and CSS.

2 thoughts on “Getting started with Moment.js: Intro with examples.2 min read

Comments are closed.