Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using Moment.js in Node.js

DZone's Guide to

Using Moment.js in Node.js

How to integrate Moment.js into your Node.js applications to utilize dates and times in different formats and use cases.

· Web Dev Zone ·
Free Resource

Learn how error monitoring with Sentry closes the gap between the product team and your customers. With Sentry, you can focus on what you do best: building and scaling software that makes your users’ lives better.

The main purpose of Moment.js is to get the current time in different formats to use them in other ways. The main reason I use it is to get the current time while making any changes in the database, so that I can change the "LastModifiedOn" property in the database and create a new row and provide a value for the "DateCreatedOn" field in the database. 

Let's get to using Moment.js in a Node.js application.

Step 1

Install moment package in the application from npm, using the following command:
npm install moment --save

This will install Moment.js in the application as well as save the js for the same.

Image title

Image title

Step 2

Now, include this Javascript in your app.js, using the code below:

var moment = require('moment');  


Step 3

The next step is to call this moment variable to get different format values. Some examples are shown here: 

Image title

A complete table, for all the moment inputs and their corresponding outputs, is stated below,

S.No

Moment Input

Output

1

moment().format('MMMM Do YYYY, h:mm:ss a')

July 8th 2016, 11:54:56 pm

2

moment().format('dddd')

Friday

3

moment().format("MMM Do YY")

Jul 8th 16

4

moment().format()

2016-07-08T23:54:56+05:30

5

moment("20111031", "YYYYMMDD").fromNow()

5 years ago

6

moment("20180620", "YYYYMMDD").fromNow()

in 2 years

7

moment().startOf('day').fromNow()

a day ago

8

moment().endOf('day').fromNow()

in 5 minutes

9

moment().startOf('hour').fromNow()

an hour ago

10

moment().subtract(10, 'days').calendar()

06/28/2016

11

moment().calendar()

Today at 11:54 PM

12

moment().add(1, 'days').calendar()

Tomorrow at 11:54 PM

13

moment.locale()

En

14

moment().format('LT')

11:54 PM

15

moment().format('LTS')

11:54:56 PM

16

moment().format('L')

07/08/2016

17

moment().format('l')

7/8/2016

18

moment().format('LL')

July 8, 2016

19

moment().format('ll')

Jul 8, 2016

20

moment().format('LLL')

July 8, 2016 11:54 PM

21

moment().format('lll')

Jul 8, 2016 11:54 PM

22

moment().format('LLLL')

Friday, July 8, 2016 11:54 PM

23

moment().format('llll')

Fri, Jul 8, 2016 11:54 PM

     

Hope this article helps you in showing or using the current time in your application.

What’s the best way to boost the efficiency of your product team and ship with confidence? Check out this ebook to learn how Sentry's real-time error monitoring helps developers stay in their workflow to fix bugs before the user even knows there’s a problem.

Topics:
nodejs development ,moment.js

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}