Doh!
The .NET 1.x routine should be more like:
Public Function IsDate(ByVal expression As String) As Boolean
Try
Dim formats() As String =
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetAllDateTimePatterns()
Dim result As DateTime = DateTime.ParseExact(expression,
formats, System.Globalization.CultureInfo.CurrentCulture,
Globalization.DateTimeStyles.AllowWhiteSpaces)
Catch ex As Exception
Return False
End Try
End Function
The:
| culture =
| System.Globalization.CultureInfo.CreateSpecificCulture("de-DE")
was verifying other cultures returned a different set of formats.
Technically I should have passed the same culture to the DateTime.ParseExact
method!
--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley -
http://www.tsbradley.net
message | MDC,
| In addition to the other comments. If you are using VS 2005 (.NET 2.0) , I
| would consider using the overload of DateTime.TryParseExact that accepts
| multiple formats:
|
|
http://msdn2.microsoft.com/h9b85w22.aspx
|
| Public Function IsDate(ByVal expression As String) As Boolean
| Dim result As DateTime
| Dim formats() As String =
|
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetAllDateTimePatterns()
| Return DateTime.TryParseExact(expression, formats,
| System.Globalization.CultureInfo.CurrentCulture,
| Globalization.DateTimeStyles.AllowWhiteSpaces, result)
| End Function
|
| Use the result variable above if you want the converted date.
|
|
| If upgrading to VS 2005 is not an option, .NET 1.x has a
DateTime.ParseExact
| that accepts multiple formats:
|
|
http://msdn.microsoft.com/library/d...l/frlrfsystemdatetimeclassparseexacttopic.asp
|
| You could wrap the call in a Try Catch to return the Boolean value.
|
| Public Function IsDate(ByVal expression As String) As Boolean
| Try
| Dim culture As System.Globalization.CultureInfo =
| System.Globalization.CultureInfo.CurrentCulture
| culture =
| System.Globalization.CultureInfo.CreateSpecificCulture("de-DE")
| Dim formats() As String =
| culture.DateTimeFormat.GetAllDateTimePatterns()
| Dim result As DateTime = DateTime.ParseExact(expression,
| formats, System.Globalization.CultureInfo.CurrentCulture,
| Globalization.DateTimeStyles.AllowWhiteSpaces)
| Catch ex As Exception
| Return False
| End Try
| End Function
|
| Of course if you want to limit the support formats you could create an
array
| of formats that you support instead of
| DateTimeFormat.GetAllDateTimePatterns...
|
|
| Be certain to read the effects of varying
| System.Globalization.DateTimeStyles values have on the call to
| DateTime.ParseExact & DateTime.TryParseExact.
|
| --
| Hope this helps
| Jay [MVP - Outlook]
| .NET Application Architect, Enthusiast, & Evangelist
| T.S. Bradley -
http://www.tsbradley.net
|
|
| || Why does this return true:
||
|| IsDate("ISometimesHateProgrammingMarch2005")
||
|| Is there another way to verify that this is not a date??
||
|| Thanks in advance!
||
|| MDC
||
|
|