我需要一些帮助,根据浏览器的语言验证Javascript中的日期时间字符串.
我可以很容易地获得日期时间格式,例如,如果语言设置为pt-BR格式将是
dd/MM/yyyy HH:mm:ss
Run Code Online (Sandbox Code Playgroud)
我试过用这样的东西:
var dateFormat = "dd/MM/yyyy HH:mm:ss";
var x = Date.parseExact($("#theDate").val(), dateFormat);
Run Code Online (Sandbox Code Playgroud)
但是x总是空的.我在想因为Date.parseExact无法做到.我需要能够为所有浏览器语言执行此操作,我宁愿不使用其他库.使用正则表达式也是因为我需要编写这么多不同的表达式.
有没有人有任何建议可以帮助我走上正轨?我也不反对使用网络方法.
我尝试使用以下webmethod,它与en-US一起使用,但没有别的:
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
Run Code Online (Sandbox Code Playgroud)