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 7982233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:37:12+00:00 2026-06-04T10:37:12+00:00

I’ve having trouble with parsing user input with Javascript, where I can’t get the

  • 0

I’ve having trouble with parsing user input with Javascript, where I can’t get the parser to accept dates in ddMMyyyy correctly. It parses correctly when there are separator characters.

The example below is using DateJS (NZ localised), and I’ve had an initial attempt with the newer MomentJs (which isn’t proving ideal for input validation). I’m open to other frameworks, if they’re going to handle input cases adequately.

My test cases:

// Parses correct value
var dateWithHyphens = Date.parse('01-06-2012');

// Parses incorrectly, using MMddyyy, instead of ddMMyyyy
var dateWithoutHyphens = Date.parse('01062012');

// Parses incorrectly, using MMddyyy, instead of ddMMyyyy
var dateWithFormat = Date.parse('01062012', { format: 'ddMMyyyy'});

I’ve created a JSFiddle for this: http://jsfiddle.net/ajwxs/1

The test cases should return 1 June, but the incorrect ones return Jan 06. (That this is input parsing – output formatting is too late).

Any suggestions on if JSDate can be better prodded to use the correct format for parsing those dates?

Update

In this application, I’m validating a number of possible user inputs, including:

01062012
01/06/2012
010612

This would make an implementation of a parseExact-style implementation a bit verbose…

  • 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-04T10:37:14+00:00Added an answer on June 4, 2026 at 10:37 am

    You want to use the Date.parseExact method. For some reason it works while the normal parse does not. Also you don’t need to pass the format option wrapped in an object.

    According to the spec Date.parse is not supposed to take a format option so that is probably why.

    // Parses correctly
    var dateWithFormat = Date.parseExact('01062012', 'ddMMyyyy');
    

    I have also updated the jsfiddle to be sure.

    Update

    I think the issue is that the parser hard-codes a bunch of simple formats and those override the localization setting for the date, month and year order in some cases. If you put the following before where you use Date it will fix your issue.

    Date.Grammar._formats = Date.Grammar.formats([
        "\"yyyy-MM-ddTHH:mm:ssZ\"",
        "yyyy-MM-ddTHH:mm:ssZ",
        "yyyy-MM-ddTHH:mm:ssz",
        "yyyy-MM-ddTHH:mm:ss",
        "yyyy-MM-ddTHH:mmZ",
        "yyyy-MM-ddTHH:mmz",
        "yyyy-MM-ddTHH:mm",
        "ddd, MMM dd, yyyy H:mm:ss tt",
        "ddd MMM d yyyy HH:mm:ss zzz",
        "ddMMyyyy",
        "MMddyyyy",
        "ddMyyyy",
        "Mddyyyy",
        "dMyyyy",
        "Mdyyyy",
        "yyyy",
        "dMyy",
        "Mdyy",
        "d"
    ]);
    

    This is not pretty but it is definitely shorter than listing all of the possible options, if only by a little bit. If you can find a less quirky Date library that might be a better option.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
We're building an app, our first using Rails 3, and we're having to build

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.