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

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.

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.

How To Add Date Using moment.js

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

Examples: Output :

Method chaining with Nodejs

Output :

How To Subtract Date Using moment.js

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

Examples: Output :

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.

Output :

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.

Output :

You can find more about on the moment official documentation.

Originally published at https://www.js-tutorials.com on December 26, 2021.

--

--

--

Hey, I am Parvez Alam. A software developer since 2009. I am a programming addict. I love learning and sharing knowledge. https://www.phpflow.com/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Create Ember APP with Firebase

Documentations Hub

Creating Swipeable Cards in Vue.js

Testing React Native apps with Linux instances on Codemagic

Comparing Frontend Frameworks for Spring Boot: React, Angular, and Vaadin (+JHipster)

Node Express Tutorial Part 5: User Authentication using JWT

REserve — Tests & Quality

Adding State to Custom HTML Elements

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Parvez Alam

Parvez Alam

Hey, I am Parvez Alam. A software developer since 2009. I am a programming addict. I love learning and sharing knowledge. https://www.phpflow.com/

More from Medium

Create Web Api with NodeJs, TypeScript and Express

How to assign default route on an Adonais js server and handle exceptions