Moment JS Difference Between Two Dates In Years, Months, Days
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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. 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, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Moment JS Humanize Duration | Moment fromNow() Example
- How to Get Next Month Date in Moment JS?
- Moment JS Get Last Date of Month Example
- Moment JS Get Month Name from Number Example
- Jquery Moment Convert String to Date Example
- JQuery Moment Add Seconds to Datetime Example
- JQuery Moment Add Year to Date Example
- JQuery Moment Subtract Month Example
- JQuery Moment Add Days to Date Example
- JQuery Moment Get Current Date Example
- How to Install and Use Moment JS in Laravel?