![]() |
VOOZH | about |
Moment.js simplifies date manipulation in Node.js by providing a powerful library for parsing, validating, manipulating, and formatting dates. It enhances handling time related operations, making complex date tasks straightforward and efficient.
In this article, we are going to see how we can use moment.js in node.js. Before using moment.js, we must ensure that we have installed the library. We can do so by using the following command.
npm i momentdependency in package.json
"dependencies": {
"moment": "^2.30.1",
}
For using Moment.js, you will need to import it into your project. This can be done in 2 ways:
import moment from 'moment';const moment = require('moment');You are now all set to use moment.js in your node.js applications. The examples below will help demonstrate using moment.js in Node.js:
Example 1: This is a code snippet that is used to date and time zone of the current day. It uses the ES6 style for importing the library.
Output:
Example 2: This example uses the require() style for importing the library.
Output: