Related Articles

Related Articles

Moment.js moment().to() Function
  • Last Updated : 24 Jul, 2020

The moment().to() function is used when users want to display a moment in relation to a time other than now. This function calculates the time to a particular date in Node.js.

Syntax:

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

Parameters: This function has two parameters, first one is of type Moment|String|Number|Date|Array which represents the date, and the second one is an optional parameter which is of boolean type and used to remove the suffix from the value.

Return Value: This function returns the date.

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Requiring the module
const moment = require('moment');
   
// Function call
var result = moment([2010, 8, 24]).to([2013, 8, 24]);
console.log(result);

chevron_right


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:

    in 3 years
    

Example 2: Filename: index.js

filter_none

edit
close

play_arrow

link
brightness_4
code

// Requiring the module
const moment = require('moment');
   
function timeTo(date){
   return moment([2011, 8, 24]).to([2017, 10, 29], true)
}
   
// Function call
var result = timeTo(moment);
console.log("Result:", result);

chevron_right


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: 6 years
    

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :