Open In App

Node.js Moment Module

Last Updated : 21 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The moment module is used for parsing, validating, manipulating, and displaying dates and times in JavaScript. 

Moment Module

The Node.js Moment module simplifies date and time manipulation. It provides an intuitive API for parsing, validating, formatting, and displaying dates and times, making it easy to handle complex date-related tasks in JavaScript applications.

Features:

  • It is easy to get started and easy to use.
  • It is widely used and popular for formatting dates and time.

Steps to Install Moment Module

You can visit the link to the Install moment module. You can install this package by using this command.

npm install moment

Updated dependencies in package.json

"dependencies": {
"moment": "^2.30.1",
}

After installing the moment module, you can check your moment version in the command prompt using the command.

npm ls moment

Output of above command

After that, you can create a folder and add a file, for example, index.js. To run this file, you need to run the following command.

node index.js

Project Structure:

project structure

Example: Demonstrates basic example of moment module.

Node
// index.js 

const moment = require('moment');

// 2020-05-08T22:57:42+05:30
console.log(moment().format());

// May 8th 2020, 10:56:31 pm
console.log(moment().format('MMMM Do YYYY, h:mm:ss a'));

// Friday
console.log(moment().format('dddd'));

// May 8th 20
console.log(moment().format("MMM Do YY"));

// 2020 escaped 2020
console.log(moment().format('YYYY [escaped] YYYY'));

Steps to run the program:

Make sure you have installed the moment module using the following commands:

npm install moment

Run the index.js file using the below command:

node index.js

Output of above command

So this is how you can use the moment module for parsing, validating, manipulating, and displaying dates and times in JavaScript.



Next Article

Similar Reads