2015 update
This is an old question, but other new questions:
close as duplicates of this, so I think itโs important to add fresh information here. I write this because I got scared thinking that people really copy and paste part of the code posted here and use it in production.
Most of the answers here either use some complex regular expressions that correspond only to some very specific formats, and actually do it wrong (for example, matching on January 32nd rather than matching the actual ISO date as advertised - see the Demo ) or they try to pass something to the Date constructor and wish them the best.
Using Moment
As I explained in this answer, the library is currently available for this: Moment.js
This is a library for analyzing, checking, processing and displaying dates in JavaScript, which has a much richer API than the standard JavaScript date processing functions.
This is 12kB minified / gzipped and works in Node.js and other places:
bower install moment --save # bower npm install moment --save # npm Install-Package Moment.js # NuGet spm install moment --save # spm meteor add momentjs:moment # meteor
Using Moment, you can be very specific about checking the correct dates. Sometimes itโs very important to add some hints about the format you expect. For example, a date like 06/22/2015 looks like a valid date unless you use the DD / MM / YYYY format, in which case this date should be rejected as invalid. There are several ways you can tell Moment which format you expect, for example:
moment("06/22/2015", "MM/DD/YYYY", true).isValid(); // true moment("06/22/2015", "DD/MM/YYYY", true).isValid(); // false
true argument is that Moment will not try to parse the input unless it exactly matches one of the provided formats (in my opinion, this should be the default behavior).
You can use the internal format:
moment("2015-06-22T13:17:21+0000", moment.ISO_8601, true).isValid(); // true
And you can use several formats as an array:
var formats = [ moment.ISO_8601, "MM/DD/YYYY :) HH*mm*ss" ]; moment("2015-06-22T13:17:21+0000", formats, true).isValid(); // true moment("06/22/2015 :) 13*17*21", formats, true).isValid(); // true moment("06/22/2015 :( 13*17*21", formats, true).isValid(); // false
See DEMO .
Other libraries
If you do not want to use Moment.js, there are other libraries:
2016 update
I created an immoment module similar to (a subset of) Moment, but without surprises caused by mutations of existing objects (see the docs for more details).
2018 update
Today, I recommend using Luxon date / time processing instead Moment, which (unlike the Moment) makes the whole object intact, so no unpleasant surprises associated with the implicit mutation dates.
More information
See also:
Rob Gravel's JavaScript Parsing Series:
Bottom line
Of course, anyone can try to invent a wheel, write a regular expression (but please actually read ISO 8601 and RFC 3339 before doing this) or call buit in the random data constructors to parse the error messages like 'Invalid Date' (Are you saying this message is exactly the same on all platforms? In all locales? In the future?), Or you can use the proven solution and use your time to improve it, and not reinvent it. All of the libraries listed here are free open source software.