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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
  1. DZone
  2. Coding
  3. JavaScript
  4. JavaScript’s Dates Are Fun With Moment.js

JavaScript’s Dates Are Fun With Moment.js

Manipulating dates in web applications is tricky, especially if your users travel across timezones. Learn how to use an open source framework to resolve this.

Swathi Prasad user avatar by
Swathi Prasad
·
Apr. 29, 17 · Tutorial
Like (7)
Save
Tweet
Share
9.63K Views

Join the DZone community and get the full member experience.

Join For Free

Date manipulation is incredibly common in most applications. It is one of those development hurdles that we all must deal with at some point in our career. We have several tools for manipulating Dates in JavaScript. Moment.js is one of those tools for JavaScript development. 

It makes dates and times easy to format, parse, validate, and internationalize. The standard JavaScript Date object isn’t too bad; it just requires writing a lot of code to do complex parsing, validation, and displaying of dates.

To begin with, link the library from cdnjs in your HTML. If you are using NodeJS, you can do “npm install moment --save.” Let’s look at some examples to get a glimpse of Moment.js.

Parsing and Validating Dates

To get a current date, just add the following line of code:

moment().format('YYYY-MM-DD');

To parse a date time string:

moment('2017-04-22 09:30:26.123');

For validating a date, we will use the isValid() method:

moment('2017-04-20 09:30:26.123').isValid();

>> true 

moment('123456').isValid();

>> false

If you know the format of an input string, we can use that to parse a date. For example:

moment("04-21-2017", "MM-DD-YYYY");

moment("04/21/2017", "MM-DD-YYYY");

Note that the parser ignores non-alphanumeric characters, so both the lines above will return the same result.

Specifying Multiple Formats

Suppose you don’t know the format of the input string, then Moment lets you provide an array of formats.

Let’s look at an example:

moment("04-21-2017", ["MM-DD-YYYY", "YYYY-MM-DD"]); // Uses the first format.

In the example above, it tries to match the input with the specified array of formats.

Check out the demo for this article on CodePen.

Conclusion

Moment.js is a great library to have if you are often working on dates in JavaScript. We have covered just some basics in this article. It has plenty of other cool features to offer. Check out their documentation and source code on GitHub.

JavaScript

Published at DZone with permission of Swathi Prasad, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The 31 Flavors of Data Lineage and Why Vanilla Doesn’t Cut It
  • Core Machine Learning Metrics
  • How to Quickly Build an Audio Editor With UI
  • Why Every Fintech Company Needs DevOps

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: