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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Related

  • Externalize Microservice Configuration With Spring Cloud Config
  • 6 Tips to Help You Write Cleaner Code in Node.js
  • Migrate Mule 3 to Mule 4 Using MMA (Mule Migration Assistant)
  • Bridging JavaScript and Java Packages: An Introduction to Npm2Mvn

Trending

  • Advanced Java Garbage Collection Concepts: Weak References, Finalization, and Memory Leaks
  • Innovation at Speed: How Cloud-Native Development Accelerates Time-to-Market
  • Java Enterprise Matters: Why It All Comes Back to Jakarta EE
  • Kung Fu Code: Master Shifu Teaches Strategy Pattern to Po – The Functional Way
  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.1K 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
  • 6 Tips to Help You Write Cleaner Code in Node.js
  • Migrate Mule 3 to Mule 4 Using MMA (Mule Migration Assistant)
  • Bridging JavaScript and Java Packages: An Introduction to Npm2Mvn

Partner Resources

×

Comments

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: