DEV Community

Chakit
Chakit

Posted on • Originally published at hashnode.chakitarora.com

What and Why - Moment.js?

Moment js is a very significant javascript library and you're definitely gonna use it! If you're a javascript developer, this is for sure.

Let's take a look at what this library does and why is it important.

Moment js is used to parse, validate, manipulate and display date/time. In short, it's the way you can play with dates in javascript. Whenever I start a new project I make sure to install moment. It solves the problem of reading different sorts of stamps and display accordingly.

You can download moment directly if you're not using any package manager or you can use yarn or npm for adding it to the dependencies. It gives a lot of things to play with. Let's look at a few of them.

You can import moment like other dependencies and use it accordingly. Moment js creates a wrapper for the Date object. To get this wrapper object, simply call "moment()" and this will give a new object with the current date config.

You can pass different sorts of parameters to parse and get the object. For example moment("2021-10-25"), or maybe something like moment("10-25-2021", "MM-DD-YYYY") and so on. There are different use cases for every time. You can even parse the timestamps.

Now to get the information out of that object, there are a lot of things we can do. You can access even the milliseconds if in the object. Then there obviously day, month, hour, day and so on!

You can even manipulate the dates, which means that you can add or subtract the dates. Maybe add delivery time to current time and see by which time the food is gonna get delivered. This is just a use-case but this should give an idea of how powerful and useful it is.

We also have a lot of different ways in which we can display od see the dates. As there are different date formats and different use cases there are different ways. Maybe you want to show 25th October 2021, or October 25th, and so on!

We can even show the time passed, for example in a chat application we need to show when the recent message is delivered - a minute ago, a few seconds ago, and so on. Or in any other application where you wanna see the files uploaded time, as in a day ago, 10 days ago, and so on!

Moment can also be used for performing other operations which might be useful, like calculating the difference between 2 dates.
There can be a lot of places where this and similar stuff is useful.

Moment also helps in querying. There are different methods available to do this. For example isBefore, isAfter, isLeapYear, and so on! The names are self-explanatory. You can compare dates and use these kinds of different methods to get the other required information.

Moment can also help with the locale, you can change the dates you're using and get every other thing according to the country and timezone! This is a great advantage when you're building for multiple places.
There is a lot more! So make sure to check the documentation.

Thank you so much for reading, I hope you liked it.

If you have any questions or suggestions, please leave a comment below or feel free to reach out.

Top comments (6)

Collapse
 
gaurang847 profile image
Gaurang

Hey! Sorry to rock the boat.
I've personally used moment.js a lot and have absolutely loved it.
But it has gone in "maintenance mode". And they officially recommend using other alternatives instead for newer projects.

momentjs.com/docs/#/-project-status/

Collapse
 
arorachakit profile image
Chakit

Thanks for the info! I am plannig to move onto new alternatives.

Collapse
 
lexlohr profile image
Alex Lohr

Horology is a difficult science and the Date object is woefully under-equipped to provide for its challenges.

However, moment.is is unmaintained now. There are a few more lightweight alternatives you may want to consider. Also, a lot of problems are easier solved than they look.

Collapse
 
jwp profile image
John Peters

Another example of a huge gap in Javascript. All Date functions should be internal.

Collapse
 
arorachakit profile image
Chakit

Looks interesting! 🚀

Collapse
 
arorachakit profile image
Chakit

I've been hearing similar things lately! Thank you :)