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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:50:08+00:00 2026-06-02T05:50:08+00:00

Please correct or explain how my over-simplification is incorrect as I am not a

  • 0

Please correct or explain how my over-simplification is incorrect as I am not a JavaScript expert.

But I just need to know if an object is a valid date. This will only come from user input (ie, text box).

var is_valid_date = function(date) {
    try {
        var d = new Date(date);
        return true;
    }
    catch(e) {
        return false;
    }
}
  • 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-02T05:50:11+00:00Added an answer on June 2, 2026 at 5:50 am

    YOU have to decide what form of dates you want to accept.

    Then, once you know what forms you want to accept, you can then check the spec for new Date(str) or date.parse() on MDN and see if it supports exactly what you want and if it does the right things on error conditions (it probably will not). If not, then you will have to do some manual parsing.

    If you want further help from us, you will need to specify what forms of date you want to accept.

    There are also some browser differences as javascript has moved to support additional date formats and earlier browsers had some inconstencies between them which all means you’ll want to build yourself a simple test script with a bunch of legal and illegal date format strings and see if your validity detection does what you want in several browsers. This isn’t rocket science to get it right, but it’s not trivial either and requires some work unless you only want to accept what the original date object supported (which is unlikely).

    If this were my code, I’d probably decide that it’s far less work to do manual parsing of your desired input format that you know with 100% certainty will work in all browsers because it’s your own manual parsing. I’d probably use a regex to parse the date and then convert each component to a number and check each component for validity. You can then feed those numeric components to the Date constructor to create the Date object.

    If you can tell by now, the built-in date class isn’t very useful for user entered input. If you’re willing to use a library for this, the date.js library has a ton of useful functionality in this regard.

    Here’s an example of a manual parsing function that accepts these US formats:

    mm-dd-yyyy
    mm dd yyyy
    mm/dd/yyyy
    

    JS Code:

    function checkDate(str) {
        var matches = str.match(/(\d{1,2})[- \/](\d{1,2})[- \/](\d{4})/);
        if (!matches) return;
    
        // parse each piece and see if it makes a valid date object
        var month = parseInt(matches[1], 10);
        var day = parseInt(matches[2], 10);
        var year = parseInt(matches[3], 10);
        var date = new Date(year, month - 1, day);
        if (!date || !date.getTime()) return;
    
        // make sure we have no funny rollovers that the date object sometimes accepts
        // month > 12, day > what's allowed for the month
        if (date.getMonth() + 1 != month ||
            date.getFullYear() != year ||
            date.getDate() != day) {
                return;
            }
        return(date);
    }
    

    And a demo with some test cases: http://jsfiddle.net/jfriend00/xZmBY/

    If you want the Euro format, it’s a trivial matter to switch the code to that. In either case, you have to decide which format you accept, code for it and then communicate to the user which format is required. If you think this is messy, then perhaps you will see why so many sites use a date calendar picker that doesn’t have this complexity.

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

Sidebar

Related Questions

Could you please explain why this code is not syntactically correct? private void addEditor(final
Please let me know whether its correct or not? I am mapping the whole
Please, explain why this code is correct or why not: In my opinion, line
Please help! I need to produce a Crystal Report with multiple data columns, but
Could someone please explain to me the correct file structure of a Symfony2 MVC
I think this one is simple just explain it a bit please... Supose I
Could someone please explain the correct usage for boost::upgrade_lock. The following code results in
Can someone please explain to a noob the correct way to animate a View
Is the following code Correct?As far as my understanding,it should not work properly,but on
please correct the program. Q. Write a java prg to accept RollNo, Name of

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.