Date or String?

vis781

Well-known member
Joined
Aug 30, 2005
Messages
2,016
Location
Cambridge, UK
Programming Experience
5-10
Hi all, here's my querie....

I have in my app at some point a string array of values about 20 or so. i was wondering if it's possible to determine, upon looping through the array, whether or not there are different formats of text within the array. For example determining if one of the items in the array is in Date format even though it's all passed as a string.
Didn't know if there was a function for this or whether i'd have to invent something?

Any help appreciated.
 
If you're using VB 2005 then you can use the Date.TryParse method. If you're using VB.NET 2003 then all you can do is use Date.Parse and catch the exception if it fails. You could use the IsDate Runtime function but it does just that anyway. It is preferable not to use IsDate generally because it will return True if the value parses to a Date, but then you have to call Parse again yourself anyway, so it is no more efficient than doing it yourself if it fails and leass efficient if it succeeds.
 
On thinking about this further, it is no doubt possible to write my own Date.Parse function using regular expressions. So when i come up with a working model i'll post the code back for your interest.
 
Ok, here it is, This will work for may app as all my dates are in the format of .. DD/MM/YYYY. So here is the regular expression that will expect this format..

VB.NET:
\d{2}/\d{2}/\d{4}

With minimal modifications it could adapted to parse any date format. Hope this is helpful to someone.
 
Back
Top