I'm struggling with validating a date time string in Javascript, based on the language set in the browser.
For example, if the language is set to pt-BR, the format would be
dd/MM/yyyy HH:mm:ss
I attempted to use the following code:
var dateFormat = "dd/MM/yyyy HH:mm:ss";
var x = Date.parseExact($("#theDate").val(), dateFormat);
However, x always returns Null. I suspect Date.parseExact doesn't support times. I need a solution that works for all browser languages without relying on another library. Regular expressions are not an option due to the complexity of writing multiple expressions.
Any suggestions on how to proceed? I'm open to using a webmethod as well.
I tested the following webmethod, which works for en-US but not other languages:
Public Function ValidateDates(ByVal strDate_In As String) As String
Dim theFormat As String = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern() + " " + CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern()
Try
Dim d As DateTime = DateTime.ParseExact(strDate_In, theFormat, CultureInfo.CurrentCulture)
Return "true"
Catch ex As Exception
Return "false"
End Try
End Function