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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • 4 Email Validation and Verification Techniques for App Developers
  • Top React Libraries for Data-Driven Dashboard App Development
  • Prototype Pattern in JavaScript
  • Decoding the Merits of MEAN Tech Stack for Your Organization

Trending

  • Unmasking Entity-Based Data Masking: Best Practices 2025
  • Agile and Quality Engineering: A Holistic Perspective
  • AI’s Role in Everyday Development
  • Docker Base Images Demystified: A Practical Guide
  1. DZone
  2. Coding
  3. Languages
  4. Getting Started With Moment.js: Introduction With Examples

Getting Started With Moment.js: Introduction With Examples

We take a quick look at how to manipulate time and dates in our web application using this open source JavaScript library.

By 
Aakash Jain user avatar
Aakash Jain
·
Nov. 13, 17 · Tutorial
Likes (14)
Comment
Save
Tweet
Share
25.4K Views

Join the DZone community and get the full member experience.

Join For Free

I think date manipulation is the most common process that every developer must deal with in his career. There are so many tools available for Date Manipulation in JavaScript like date.js, date-fns and so many interesting libraries. Moment.js is one of them.

The developer can easily interact with date and time domain problems by using Moment.js. JavaScript Date requires lines of code for parsing, manipulation, and validation.

Instead of modifying the native Date.prototype, Moment.js creates a wrapper for the Date object. To get this wrapper object, simply call moment() with one of the supported input types. Moment.js has a very flexible and advanced parser that allows a huge range of functionality.

Before we begin, include the library from cdnjs in your HTML code.

To get current date and time, just call moment() with no arguments.

var currentTime = moment(); //same as calling moment(new Date());

Parsing and Validation of Dates

  • To get the date in a few different formats:
var date = moment.format("MM-DD-YYYY"); // will return in MM-DD-YYYY format
var date2 = moment.format("DD/MM/YYYY"); // will return in DD/MM/YYYY format
  • To parse date time string:
moment("03-06-1995", "MM-DD-YYYY");

The parser ignores non-alphanumeric characters, so both of the following lines of code will return the same thing.

moment("03-06-1995", "MM-DD-YYYY");
moment("03/06/1995", "MM-DD-YYYY");
  • For validation of the date, we can use the isValid() function.
moment.isValid(); //return true
var m = moment("2011-25-08T12:65:20");
m.isValid(); // return false
m.invalidAt(); // return 4 for minutes

The return value for invalidAt() has the following meaning :

  • 0 for years, 1 for months, 2 for days, 3 for hours, 4 for minutes, 5 for seconds,6 for milliseconds.

Manipulation of Dates

Once you have a Moment, you can manipulate the date in some way. There are a number of methods to help with this. Moment.js uses method chaining.

You can add, subtract or set your app to the start or end of the day, month and year. For example:

moment().add(360,'days'); // add 360 Days to current Date
moment().subtract(7,'months') // subtact 7 months from current Date
moment().startOf('year');// set to January 1st, 12:00 am this year
moment().endOf('year');// set the moment to 12-31 23:59:59.999 this year

As you all have seen, Moment.js is a great library for working with dates in JavaScript. We have covered just some of the basics in this article. It has lot of other cool features to offer. Check out their documentation and source code on GitHub.

Thanks!

JavaScript Library dev Parser (programming language) Object (computer science) app Moment career Strings

Published at DZone with permission of Aakash Jain, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • 4 Email Validation and Verification Techniques for App Developers
  • Top React Libraries for Data-Driven Dashboard App Development
  • Prototype Pattern in JavaScript
  • Decoding the Merits of MEAN Tech Stack for Your Organization

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
  • support@dzone.com

Let's be friends: