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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:24:15+00:00 2026-05-26T19:24:15+00:00

Possible Duplicate: Detecting an “invalid date” Date instance in JavaScript I was using the

  • 0

Possible Duplicate:
Detecting an “invalid date” Date instance in JavaScript

I was using the following to detect a valid date:

var text = $('#Date').val();
var date = Date.parse(text);

if (isNaN(date)) {
      // Invalid date
}

But found that Date.parse thinks the following are valid dates (mm/dd/yyyy)

  • 2/30/2011
  • 11/31/2011

Any other way to detect invalid dates when the number of days surpasses the total number of
days in the month?

UPDATE: An even larger problem is that the jQuery validation plugin doesn’t detect this as an invalid date either!

SOLUTION:

Based on @Guffa’s comments I have created the following function to validate dates:

function validDate(text) {

    var date = Date.parse(text);

    if (isNaN(date)) {
        return false;
    }

    var comp = text.split('/');

    if (comp.length !== 3) {
        return false;
    }

    var m = parseInt(comp[0], 10);
    var d = parseInt(comp[1], 10);
    var y = parseInt(comp[2], 10);
    var date = new Date(y, m - 1, d);
    return (date.getFullYear() == y && date.getMonth() + 1 == m && date.getDate() == d);
}
  • 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-26T19:24:15+00:00Added an answer on May 26, 2026 at 7:24 pm

    To check if a date is valid you can parse the components of the date, create a Date object from it, and check if the components in the data is the same as the parsed components. If you create a Date object from compnents that are out of range, the values will flow over to the next/previous period to create a valid date.

    For example, new Date(2011,0,42) will create an object that contains the date 2/11/2011 instead of 1/42/2011.

    By parsing the components instead of the full date you will also get around the problem with different date formats. My browser will for example expect a date format like y-m-d rather than d/m/y.

    Example:

    var text = '2/30/2011';
    var comp = text.split('/');
    var m = parseInt(comp[0], 10);
    var d = parseInt(comp[1], 10);
    var y = parseInt(comp[2], 10);
    var date = new Date(y,m-1,d);
    if (date.getFullYear() == y && date.getMonth() + 1 == m && date.getDate() == d) {
      alert('Valid date');
    } else {
      alert('Invalid date');
    }
    

    Demo: http://jsfiddle.net/Guffa/UeQAK/

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

Sidebar

Related Questions

Possible Duplicate: JavaScript for detecting browser language preference I want to detect the language
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} What's the
Possible Duplicate: Detecting an undefined object property in JavaScript Would it be like this?
Possible Duplicate: Detecting an undefined object property in JavaScript From the below JavaScript sample,
Possible Duplicate: Custom alert using Javascript I want to create a confirmation message-Do you
Possible Duplicates: window.open not working in IE Javascript “window.open” code won't work in Internet
Possible Duplicate: Detecting IE using jQuery Hello, I'm trying to figure out how to
Possible Duplicate: Detecting moved files using FileSystemWatcher I'm trying to monitor moved files with
Possible Duplicate: Detecting support for a given JavaScript event? having a random HTMLElement el
Possible Duplicate: iOS - Detecting whether or not device support phone calls? I'm writing

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.