Open In App

Moment.js moment().diff() Function

Last Updated : 08 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The moment().diff() function is used to get the difference in milliseconds of given dates which are passed as parameters to this function. 

Syntax:

moment().diff(Moment|String|Number|Date|Array, String, Boolean);

Parameters: This function has two parameters, The first one is the date of type Moment|String|Number|Date|Array, and the second parameter is an optional parameter of the boolean type which is used to get floating numbers as a result instead of integers. 

Return Value: This function returns the date in milliseconds.

Steps to create the application:

Step 1: You can install this package by using this command.

npm install express

Step 2: After installing the express module, you can check your express version in the command prompt using the command.

npm version express

Project Structure:

NodeProj

The updated dependencies in package.json file will look like:

"dependencies": {
"express": "^4.18.2",
}

Example 1: Below is the code example of moment().diff() Function

Javascript




// Requiring the module
const moment = require('moment');
 
let dateOne = moment([2019, 03, 17]);
let dateTwo = moment([2001, 10, 28]);
 
// Function call
let result = dateOne.diff(dateTwo, 'days')
 
console.log("No of Days:", result)


Steps to Run the Program:

node index.js

Output:

No of Days: 6349

Example 2: Below is the code example of moment().diff() Function

Javascript




// Requiring the module
const moment = require('moment');
 
function getYearDiff(dateOne, dateTwo) {
    return dateOne.diff(dateTwo, 'years', true);
}
 
// Function call
let result = getYearDiff(moment([2019, 11,
    30]), moment([2001, 2, 17]));
 
console.log("No of years difference:", result)


Steps to Run the Program:

node index.js

Output:

No of years difference: 18.78611111111111


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads