Time and time again I’m reminded that you can’t escape time and so are the problems related to date and time. One such very common problem is to find the difference between 2 dates.
Let me give you a very simple example where we will try to find the how many days old are you?
So it feels like not a such big deal but think of writing the entire logic on your own when you know you can write but it will much less of a hassle to do it via library like moment.
I’m writing this as an article here because it took me time to find the way to do it using moment itself. I’m writing this article more for myself than for others. So consider this as a treat.
Here’s the code:
let myDob = moment("19/12/1997", "DD/MM/YYYY");
let today = moment();
let myAge = today.diff(myDob, "years");
let noOfDays = today.diff(myDob, "days");
Below is the embedded sandbox to demonstrate it:
Check out the same in moment docs: moment
Thanks for your time reading this. I hope it helped you. Have a productive day :)
PS:Moment is in maintenance mode.
You can read about the meaning of it in the article linked below. MOMENT.JS OFFICIALLY BECOMES A LEGACY PROJECT IN MAINTENANCE MODE
Originally published at Klickspell Engineering
Need technical precision for your web stack?
Architected for performance & scale.
From modern headless commerce to enterprise infrastructure and clean frontend state systems, Klickspell delivers senior engineering craft without agency bloat.
Keep Reading
Related Engineering Articles
5 Frequently used JS libraries for React
I've seen many interns that join my company who have just basic knowledge of JS and are starting to...
Why JavaScript not Python for Web Development?
PS: Web Development term here primarily concentrated on Frontend Development. So today one of my fri...