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

Jquery

Nicesnippets

131

30-04-2021


Hi Guys,

In this blog, I will show you how to get difference between two dates in years, month and days using moment js. We will talk about moment js difference between two dates in years, months, days example.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.

Here i will give you full example for moment js difference between two dates in years, months, days example. So let's see the bellow example :

Example


<!DOCTYPE html>

<html>

<head>

<title>jquery moment example - NiceSnippets.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 - NiceSnippets.com</h1>

</body>

<script type="text/javascript">

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

var endDate = moment('02-05-2021', '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

It will help you....


Recommended Posts