Moment JS Difference Between Two Dates In Years, Months, Days

By Hardik Savani | January 7, 2021 | Category : jQuery


Hi,

This article will provide example of moment js difference between two dates in years. i explained simply step by step months days. if you have question about moment difference between two dates in days then i will give simple example with solution. i explained simply step by step moment difference between two dates in months.

Here, i will give you simple example of jquery moment js difference between two dates in years months days using diff().

let's see both example with output:

Example:

<!DOCTYPE html>

<html>

<head>

<title>jquery moment example - ItSolutionStuff.com</title>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" crossorigin="anonymous"></script>

</head>

<body>

<h1>jquery moment example - ItSolutionStuff.com</h1>

</body>

<script type="text/javascript">

var startDate = moment('02-01-2021', 'DD-MM-YYYY');

var endDate = moment('02-05-2022', 'DD-MM-YYYY');

var dayDiff = endDate.diff(startDate, 'days');

console.log('Days:' + dayDiff);

var monthDiff = endDate.diff(startDate, 'months');

console.log('Month:' + monthDiff);

var yearDiff = endDate.diff(startDate, 'years');

console.log('Year:' + yearDiff);

</script>

</html>

Output:

Days:485

Month:16

Year:1

I hope it can help you...



Tags : jQuery Moment JS


Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: