DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Externalize Microservice Configuration With Spring Cloud Config
  • Bridging JavaScript and Java Packages: An Introduction to Npm2Mvn
  • Next Generation Front-End Tooling: Vite
  • Node.js: Architectural Gems and Best Practices for Developers to Excel

Trending

  • Every Cache Miss Is a Tiny Tax on Your Performance
  • Identity in Action
  • Mastering Fluent Bit: Beginners' Guide for Contributing to Our CNCF Project Website
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  1. DZone
  2. Coding
  3. JavaScript
  4. Using Moment.js in Node.js

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.

By 
Vipul Malhotra user avatar
Vipul Malhotra
·
Jul. 27, 16 · Opinion
Likes (3)
Comment
Save
Tweet
Share
93.4K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Node.js Moment Database application Npm (software) Command (computing) JavaScript Property (programming)

Opinions expressed by DZone contributors are their own.

Related

  • Externalize Microservice Configuration With Spring Cloud Config
  • Bridging JavaScript and Java Packages: An Introduction to Npm2Mvn
  • Next Generation Front-End Tooling: Vite
  • Node.js: Architectural Gems and Best Practices for Developers to Excel

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook