Skip to content
Related Articles

Related Articles

Moment.js moment().format() Function
  • Last Updated : 29 Jul, 2020

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:



  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');
   
// The format() function to format the date 
var formatedDate = moment().format(
    "dddd, MMMM Do YYYY, h:mm:ss a");
console.log(formatedDate);

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:

    Friday, July 17th 2020, 4:28:30 pm
    

Example 2: Filename: index.js

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


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: Friday, July 17th 2020
    

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :