Skip to main content
Moment JS with Example Using Node.js

Moment JS with Example Using Node.js

in this post, we’ll learn about the moment js library for validating, parsing and manipulating dates with times.

It’s always been a challenge to work with dates and timings. The JavaScript library for handling dates has always seemed useful to me.

what is moment js

The Moment.js is a fantastic JavaScript package that allows you to manipulate dates in the browser, You can use this lib with angular, react, node.js etc. In Moment.js, the moment object is mutable. It indicates that operations such as add, subtract, and set modify the original moment object.

Shorthand Keys

Key Shorthand
years y
quarters Q
months M
weeks w
days d
hours h
minutes m
seconds s
milliseconds ms

How To Use Moment JS with Node.js

Let’s create a nodejs app to implement the moment.js library.

Create Project and Install Dependencies

We’ll create a project using following command:

mkdir test-moment
cd test-moment

Now, Install moment.js using the following command:

npm install moment --save

Create a file called server.js in the root directory and add the following code to it.

const moment = require('moment');

console.log(moment().format('DD/MM/YYYY'));

Let’s start the node.js server

node server.js

The moment() returns the current date and time, whereas format() converts the current date and time to the format supplied.

Output :
14/12/2021

How To Add Date Using moment.js

Let’s add some days, months, and years into a given date.

Syntax :

moment().add(Number, String);
moment().add(Duration);
moment().add(Object);

Examples:

// 10 days is added to the current date
const days = moment().add(10, 'days').format('DD/MM/YYYY');
// 2 months is added to current date
const months = moment().add(2, 'months').format('DD/MM/YYYY');
// 2 years is added to current date;
const years = moment().add(2, 'years').format('DD/MM/YYYY');

console.log('Days', days);
console.log('Months', months);
console.log('Years', years);

Output :

Days 24/12/2021
Months 14/02/2022
Years 14/12/2023

Method chaining with Nodejs

// using chaining method
console.log(moment().add(2, 'days').add(2, 'months'));
// with object literal
console.log(moment().add({days:3,months:3}));

Output :

Moment<2022-02-16T08:33:04+05:30>
Moment<2022-03-17T08:33:04+05:30>

How To Subtract Date Using moment.js

Let’s add some days, months, and years into a given date.

Syntax :

moment().subtract(Number, String);
moment().subtract(Duration);
moment().subtract(Object);

Examples:

// 10 days is subtract to the current date
const days = moment().subtract(10, 'days').format('DD/MM/YYYY');
// 2 months is subtract to current date
const months = moment().subtract(2, 'months').format('DD/MM/YYYY');
// 2 years is subtract to current date;
const years = moment().subtract(2, 'years').format('DD/MM/YYYY');

console.log('Days', days);
console.log('Months', months);
console.log('Years', years);

Output :

Days 04/12/2021
Months 14/10/2021
Years 14/12/2019

Difference Between Dates Using moment.js

The diff() method in the js library is used to compare two dates. It takes a date as its first argument. A second input can be used to specify the time unit.

const dateA = moment('2021-12-10');
const dateB = moment('2019-09-10');

console.log('Difference is ', dateA.diff(dateB, 'days'), 'days');
console.log('Difference is ', dateA.diff(dateB, 'months'), 'months');
console.log('Difference is ', dateA.diff(dateB, 'years'), 'years');

Output :

Difference is  822 days
Difference is  27 months
Difference is  2 years

Moment.js Date Comparison Methods

The moment.js library have isBefore(), isAfter(), and isSame() methods for comparing dates. These methods return Boolean values that are indicating if one date is before, after, or equal to another date.

console.log(moment('2021-12-14').isAfter('2021-11-17'));
console.log(moment('2021-12-14').isAfter('2021-12-15'));

Output :

true
false

You can find more about on the moment official documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *