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

  • Home
  • SEARCH
  • 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 6872365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:55:07+00:00 2026-05-27T03:55:07+00:00

Who will answer my question? How can I validate a date using Javascript cross

  • 0

Who will answer my question?

How can I validate a date using Javascript cross browser?

I use a invalid date: 2011-11-31 like below:

var d = new Date("2012-11-31")

In FF:

d = NaN // Date is invalid

In Chrome:

d.getFullYear();//2012
d.getDate();//1
d.getMonth();//11
  • 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-05-27T03:55:07+00:00Added an answer on May 27, 2026 at 3:55 am

    Tested in IE7-9, chrome, opera, safari, firefox. Test here: http://jsfiddle.net/MRwAq/

    You could do it like this:

    (function(){
                                 //This should be good enough initial filter, let the native Date decide if the number are valid
        var validStringRE = /^([0-9]{4})-([0-1][0-9])-([0-3][0-9])$/;
    
        function pad( num ) {
        return num < 10 ? "0"+num : num;
        }
    
        Date.prototype.getISOFormat = function(){
        return this.getFullYear() + "-" +
        pad( ( this.getMonth() + 1 ) ) + "-" +
        pad( this.getDate() );
        };
    
        function isValidISODate( date ) {
        var matches, a;
            if( !validStringRE.test( date.toString() ) ) {
            return false; //Get rid of anything but "NNNN-NN-NN"
            }
    
        matches = date.match( validStringRE );
    
        a = new Date( +matches[1], +matches[2] - 1, +matches[3], 0, 0, 0, 0 );
    
            if( isNaN( a ) ) {
            return false; //firefox, ie
            }
    
            if( a.toString().toLowerCase() === "invalid date" ) {
            return false; //chrome in some cases, opera, safari
            }
    
        return a.getISOFormat() === date; //browsers that "conveniently" calculate
        }
    
    window.isValidISODate = isValidISODate;
    })()
    

    Then:

    var isValid = isValidISODate("2012-11-31");
    //false
    
    var isValid = isValidISODate("2012-11-30");
    //true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a student who will be using C++ next quarter. I really enjoyed
Anyone who's tried to study mathematics using online resources will have come across these
Alleged interview question and answer here . Will the following code compile (in C)?
Update: I can't delete this question, because the answer has been upvoted, yet it
as Content = Content Builder ( Who will convert Client's Content (MS Word files)
I'm looking for a feature or software, who will allow me to easily profile
Currently, I'm in the process of hiring a web developer who will be working
My current solution will suck sometimes EDIT For those who don't understand,see this example:
Is there a web-service/API that will provide information on who the US State and
I'm having trouble coming up with a query that will find all customers who

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.