What and Why - Moment.js?

DevRel 🥑 at Storyblok. I am a full stack developer with knowledge and experience in several front-end techs like Angular, React, Vue along with backend techs like Node, Flask, FastAPI, etc.
Search for a command to run...

DevRel 🥑 at Storyblok. I am a full stack developer with knowledge and experience in several front-end techs like Angular, React, Vue along with backend techs like Node, Flask, FastAPI, etc.
No comments yet. Be the first to comment.
Every time I decide to be consistent about content creation, I get (or make) opportunities to travel, explore, and network. It could be procrastination or a matter of priorities but if you are a devrel or wanna be a devrel, you already know how much ...

You must have seen the progress of Tesla and SpaceX in the past few years. We all have seen both of the companies going through ups and downs but what I found more interesting is the way Elon Musk decided to be open about both of the products with th...

Well, the purpose of this blog post is to share what I was up to for the past few months but before we dive into that, let me refresh your memory a little. A few months ago, I took it upon myself to use Javascript in every place I could and I am stil...

Tailwind is an amazing CSS framework and is growing at a rapid pace. Here are some UI kits to help you make great designs. 1 - Tail-Kit Contains a lot of ready-to-use components with some templates! It's open-source! Link - Tail-kit 2 - Wicked ...

Recently a lot of people got access to Github Copilot, I was one of those who got it. I was on the waiting list for a very long time, and trust me I was dying to use it after all the good stuff already heard. Github Copilot is an AI pair programmer d...

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.
If you have any questions or suggestions, please leave a comment below or feel free to reach out.