. Advertisement .
..3..
. Advertisement .
..4..
If you learn about the figures related to date, month and years by applying moment.js and have a headache with this trouble. Our post helps you to get the difference between two dates in days, month, and years running moment.js. Reading below parts to explore the simple method for this question!
What is MomentJS?
MomentJS is considered as a library’s JavaScript, it assists to parse, validate, manipulate and display the date or time in JavaScript. It allows displaying the date in a readable format for humans and each localization. MomentJS can be used within a brower applying the script’s method. Moreover, It can apply with Node.js and is installed using npm.
For MomentJS, there are many easy approaches to insert, justify, subtract the date and get the date’s maximum or minimum etc. On the other hand, you can develop to the library easily by inserting in the plugins’ form and making it accessible in Node.js and GitHub.
In the MomentJS, you want to add the contrast between two dates in days, month and year, see the solution below. Firstly, we can give an example to solve it, like the difference from 04/05/2014 to 22/02/2013 to adjusted as one Year, one Month and fourteen days.
Solution to get the difference between two dates in days, month, and years running moment.js
Solution 1: Utilize the duration.asHours() method
You can get the difference between two dates in days, month, and years running moment.js by simply utilizing the duration.asHours() method. Look at following commands:
var duration = moment.duration(end.diff(startTime)); var hours = duration.asHours();
Solution 2: Utilize diff() function
Moment.js provides a method to get the difference between two dates. The calculated difference can optionally be returned in days, months, years, etc. in addition to milliseconds as default. Use the diff() function to calculate the difference. A date is the method’s first argument. The second input, which is optional, can be used to specify the time unit. If this is left out, milliseconds are used instead. The use of diff() is demonstrated in the example and demo that follow.
const dateB = moment('2019-11-11');
const dateC = moment('2019-10-11');
console.log(`Difference is ${dateB.diff(dateC)} milliseconds`);
console.log(`Difference is ${dateB.diff(dateC, 'days')} day(s)`);
console.log(`Difference is ${dateB.diff(dateC, 'weeks')} week(s)`);
console.log(`Difference is ${dateB.diff(dateC, 'months')} month(s)`);
Output
Difference is 2678400000 milliseconds
Difference is 31 day(s)
Difference is 4 week(s)
Difference is 1 month(s)
Solution 3: Include the both of plugin with readable-range.js and moment.js in the program
You can get the difference from two moments, however, the value has elapsed in milliseconds. Moreover, moment has an object of duration, but it determines a fixed day in the month of thirty days. As you know it isn’t always the type.
A plugin created for the moment is already named “Precise Range” and does the right result. Here is the link to find other user’s similar answers, it calculates the right figure of days to modify within the month.
After that, you include the both of plugin with readable-range.js and moment.js in the program as follow:
var m1 = moment('2/22/2013','M/D/YYYY');
var m2 = moment('4/5/2014','M/D/YYYY');
var diff = moment.preciseDiff(m1, m2);
console.log(diff);
Conclusion
Finally, the approach provided above is straightforward and answers how to get the difference between two dates in days, month, and years running moment.js. Please share any further questions in the comments box if you have any concerns. I wish you an advantageous day with the moment.js functions.
Read more
→ Compare Dates Moment js: Solutions to compare only dates in Moment.js
Leave a comment