Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8012811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:26:47+00:00 2026-06-04T19:26:47+00:00

Is MomentJS intended for user input parsing? I’ve got moderately close with the easy

  • 0

Is MomentJS intended for user input parsing?

I’ve got moderately close with the easy cases, having it accept dates in the DDMMYYYY order, and it handles some variation.

It doesn’t handle invalid dates particularly well when specifying the format – Including having day values too high, or switching year values between 2 and 4 digit.

Examples of year interpretation:

var date1 = moment('30082012', 'DDMMYYYY');
var date2 = moment('30082012', 'DDMMYY'); // Gives wrong year - 2020
var date3 = moment('300812', 'DDMMYYYY'); // Gives wrong year - 1900
var date4 = moment('300812', 'DDMMYY');

Examples of what would hopefully be invalid dates:

var date5 = moment('08302012', 'DDMMYYYY'); // Gives Jun 08 2014
var date6 = moment('08302012', 'DDMMYY'); // Gives Jun 08 2022
var date7 = moment('083012', 'DDMMYYYY'); // Gives Jun 08 1902
var date8 = moment('083012', 'DDMMYY'); // Jun 08 2014

I have created a JS Fiddle with these examples: http://jsfiddle.net/cHRfg/2/

Is there a way to have moment accept a wider array of user input, and reject invalid dates? Or is the library not intended for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:26:49+00:00Added an answer on June 4, 2026 at 7:26 pm

    You can try parsing multiple formats. Updated fiddle: http://jsfiddle.net/timrwood/cHRfg/3/

    var formats = ['DDMMYYYY', 'DDMMYY'];
    var date1 = moment('30082012', formats);
    var date4 = moment('300812', formats);
    

    Here are the relevant docs. http://momentjs.com/docs/#/parsing/string-formats/

    There is development on adding moment.fn.isValid which will allow you to do validation like in examples 5-8. It will be added in the 1.7.0 release. https://github.com/timrwood/moment/pull/306

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've having trouble with parsing user input with Javascript, where I can't get the
I enjoy developing for the web, HTML/CSS/JavaScript are easy to debug and see what
I've got about 100 unit tests and with a coverage of %20, which I'm
Having one of those 'I'm clearly doing something wrong' moments. It feels like I'm
Coming from LISP I am having my most challenging moments when accessing java objects.
So I saw this question a few moments ago on SO and it got
I found the Momentjs library which is pretty cool, however I don't find the
I have a table with data relating to several moments in time that I
Possible Duplicate: Why is String final in Java? There are various moments in my
I know that I can't get a specific answer to my question, but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.