Skip to content Skip to sidebar Skip to footer

Javascript Equivalent To .net's Datetime.parse

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

Solution 1:

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-2004Date.parseExact("10/15/2004", ["M/d/yyyy", "MMMM d, yyyy"]);

Solution 2:

In all honesty, your best bet is to perform an AJAX hit, and ask your ASP.net web-server to parse the string and return a Javascript date.

Javascript libraries easily get confused with different locales, e.g.:

GET/ParseDate.ashx?dateStaring=06/01/344:53:05 غ.و&locale=ar-SA

Which gets really complicated because:

"6/1/34" = November 19, 2012

The .NET framework, with Windows behind it, has support for a lot of different locales.

Solution 3:

Instead of trying to find two Datetime implementations (one for JS and another for C#) that have similar validation and parsing, have you considered having the client 1)use its own library to validate the date and 2)parse and reformat the date to a C# friendly format?

This would allow you to use DateJS to get a very flexible front end for date inputs, make it easier to deal with the client side culture, and let your server side deal with a fixed format.

Solution 4:

Have you tried passing your string into the constructor?

Here's a sample from https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date

var birthday = newDate("December 17, 1995 03:24:00");

Post a Comment for "Javascript Equivalent To .net's Datetime.parse"