Moment.js moment().diff() Function

29 Jul, 2020
The moment().diff() function is used to get the difference in milliseconds of given dates which are passed as parameter to this function.
Syntax:
moment().diff(Moment|String|Number|Date|Array, String, Boolean);
Parameters: This function has two parameters, first one is the date of type Moment|String|Number|Date|Array and second parameter is an optional parameter of boolean type which is used to get floating numbers as result instead of integer.
Return Value: This function returns the date in milliseconds.
Installation of moment module:
You can visit the link to Install moment module. You can install this package by using this command.npm install momentAfter installing the moment module, you can check your moment version in command prompt using the command.npm version momentAfter that, you can just create a folder and add a file for example, index.js as shown below.
You can visit the link to Install moment module. You can install this package by using this command.npm install moment
npm install moment
After installing the moment module, you can check your moment version in command prompt using the command.npm version moment
npm version moment
After that, you can just create a folder and add a file for example, index.js as shown below.
Example 1: Filename: index.js
// Requiring the moduleconst moment = require('moment');  var dateOne = moment([2019, 03, 17]);var dateTwo = moment([2001, 10, 28]);  // Function callvar result = dateOne.diff(dateTwo, 'days')   console.log("No of Days:", result)
Steps to run the program:
The project structure will look like this:Run index.js file using below command:node index.jsOutput:No of Days: 6349

The project structure will look like this:
Run index.js file using below command:node index.jsOutput:No of Days: 6349

node index.js
Output:
No of Days: 6349

Example 2: Filename: index.js
// Requiring the moduleconst moment = require('moment');   function getYearDiff(dateOne, dateTwo) {   return dateOne.diff(dateTwo, 'years', true);}   // Function callvar result = getYearDiff(moment([2019, 11,     30]), moment([2001, 2, 17]));  console.log("No of years difference:", result)
Steps to run the program:
The project structure will look like this:Run index.js file using below command:node index.jsOutput:No of years difference: 18.78611111111111

The project structure will look like this:
Run index.js file using below command:node index.jsOutput:No of years difference: 18.78611111111111

node index.js
Output:
No of years difference: 18.78611111111111

Reference: https://momentjs.com/docs/#/displaying/difference/
Moment.js
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Node.js fs.writeFile() Method
How to install the previous version of node.js and npm ?
Difference between promise and async await in Node.js
Mongoose | findByIdAndUpdate() Function
Installation of Node.js on Windows
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?
Differences between Functional Components and Class Components in React