Open In App

Moment.js moment().from() Function

Last Updated : 20 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The moment().from() function is used to calculate the time from any other time in Node.js. It takes a date as a parameter and calculates the time from that date. 

Syntax:

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

Parameters: This function accepts two parameters, first is the date parameter which can be of Moment|String|Number|Date|Array type. And second is an optional boolean parameter to return the value without the suffix. 

Return Value: This function returns the date. 

Installation of moment module:

You can visit the link to Install moment module. You can install this package by using this command.

npm install moment

After installing the moment module, you can check your moment version in the command prompt using the command.

npm version moment

Project Structure:

After that, you can create a folder and add a file, for example, index.js as shown below.

Example 1: Filename: index.js 

javascript




// Requiring the module
const moment = require('moment');
  
// Function call
let result = moment([2010, 8, 24]).from([2017, 1, 25]);
console.log(result);


Steps to run the program:

Run the index.js file using the below command:

node index.js

Output:

6 years ago

Example 2: Filename: index.js 

javascript




// Requiring the module
const moment = require('moment');
  
function timeFromDate(date){
   return moment([2011, 8, 24]).from([2019, 8, 24]);
}
  
let result = timeFromDate(moment);
console.log("Result:", result);


Steps to run the program:

Run the index.js file using the below command:

node index.js

Output:

Result: 8 years ago

Reference: https://momentjs.com/docs/#/displaying/from/


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

Similar Reads