Moment.js moment().to() Function
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:
You can visit the link to the 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 just 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]).to([2013, 8, 24]); console.log(result); |
Steps to run the program:
Run the index.js file using the below command:
node index.js
Output:
in 3 years
Example 2: Filename: index.js
javascript
// Requiring the module const moment = require( 'moment' ); function timeTo(date){ return moment([2011, 8, 24]).to([2017, 10, 29], true ) } // Function call let result = timeTo(moment); console.log( "Result:" , result); |
Steps to run the program:
Run the index.js file using the below command:
node index.js
Output:
Result: 6 years
Reference: https://momentjs.com/docs/#/displaying/to/
Contact Us