Javascript equivalent to .NET's DateTime.Parse
asp.net-mvc, asp.net-mvc-3, c#, javascript, jquery
Solution
Datejs seems pretty robust to me. Its parse function supports over 150 cultures:
Date.parse("February 20th 1973")
And in case you need to parse a date string that is not valid in the current culture you can use the parseExact function:
// The Date of 15-Oct-2004
Date.parseExact("10/15/2004", ["M/d/yyyy", "MMMM d, yyyy"]);
Problem
I'm trying to build a validator that will work with .NET's DefaultModelBinder of using DateTime.Parse to convert a string from the form post to a DateTime. I don't want to have to wait until a date has been posted to the server for it to realize it was a bad date. Currently jquery.validate uses the following code to validate date fields: ``` // http://docs.jquery.com/Plugins/Validation/Methods/date date: function(value, element) { return this.optional(element) || !/Invalid|NaN/.test(new Date(value)); } ``` However, due to Javascript's terrible Date parser, this: 275481/69/100089 Will evaluate as valid, to Sep. 12, 275760. While on the other hand, this: 11-19-2013 Will evaluate as invalid. Of course, I understand that C#'s DateTime.Parse() takes things like culture (localization) and leap year into account, and I could live with assuming a fixed (US) culture, and allowing "02-29-2013" on the client and kick it out at the server (ideally not, but it's acceptable). But I can't believe someone hasn't put together a better date validator to work with C#'s DateTime.Parse() logic. Maybe someone has, I just haven't found it -- which is why I'm posting here. And I know I have several ways to go about this -- from incredibly simple (less accurate) to incredibly complex (more accurate), but I'm hoping someone has already gone down this road and found the sweet spot.