Work About Team Blog Tutorials FAQ
Book a discovery call → Chat on WhatsApp

Home / Blog / React & Frontend / Find No of days between 2 dates using moment.js

javascriptwebdevbeginnershelp

Find No of days between 2 dates using moment.js

💡
Cross-posted content: This article is also published on Dev.to. View original on Dev.to ↗

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

Full-Stack Web 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.

Book Free Technical Call → Explore Headless Engineering ↗

Keep Reading

Related Engineering Articles

javascriptwebdev

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...

3 min read Read →
webdevpython

Why JavaScript not Python for Web Development?

PS: Web Development term here primarily concentrated on Frontend Development. So today one of my fri...

3 min read Read →

Technical Blog

Explore all 50+ engineering tutorials

All Articles ↗

Looking for custom
Shopify engineering craft?

Let's build
your store right.

Book a free 30-minute call. We'll talk through your store goals, timeline, and budget.

Free 30-min call · Discuss scope & budget

Book your free call → Chat on WhatsApp

The craft in every click.