Moment.js moment().format() Function
The moment().format() function is used to format the date according to the user need. The format can be provided in string form which is passed as a parameter to this function.
Syntax:
moment().format(String);
Parameters: This function accept single parameter of string type, which defines the format.
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 command prompt using the command.
npm version moment
- 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' ); // The format() function to format the date var formatedDate = moment().format( "dddd, MMMM Do YYYY, h:mm:ss a" ); console.log(formatedDate); |
chevron_right
filter_none
Steps to run the program:
- The project structure will look like this:
- Run index.js file using below command:
node index.js
Output:
Friday, July 17th 2020, 4:28:30 pm
Example 2: Filename: index.js
// Requiring the module const moment = require( 'moment' ); function format_Date(date){ return moment().format( "dddd, MMMM Do YYYY" ); } var result = format_Date(moment); console.log( "Result:" , result); |
chevron_right
filter_none
Steps to run the program:
- The project structure will look like this:
- Run index.js file using below command:
node index.js
Output:
Result: Friday, July 17th 2020