Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Moment.js moment().fromNow() Function

  • Last Updated : 24 Jul, 2020

The moment().fromNow() function is used to calculate the time from now in Node.js. This function returns the time, spend from the date which is passed in parameter.

Syntax:

moment().fromNow(Boolean);

Parameters: This function has one optional boolean parameter which is passed to get the value without the suffix.

Return Value: This function returns the date calculated.

Installation of moment module:



  1. You can visit the link to Install moment module. You can install this package by using this command.
    npm install moment
  2. After installing the moment module, you can check your moment version in command prompt using the command.
    npm version moment
  3. 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 module
const moment = require('moment');
   
// Function call
var result = moment([2010, 8, 24]).fromNow();
console.log(result);

Steps to run the program:

  1. The project structure will look like this:
  2. Run index.js file using below command:
    node index.js

    Output:

    10 years ago
    

Example 2: Filename: index.js




// Requiring the module
const moment = require('moment');
   
function timeFromNow(date){
   return moment([2015, 8, 24]).fromNow(true);
}
   
// Function call
var result = timeFromNow(moment);
console.log("Result:", result);

Steps to run the program:

  1. The project structure will look like this:
  2. Run index.js file using below command:
    node index.js

    Output:

    Result: 5 years
    

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :