IsDate replacement

IsDate replacement

View member feedback to this tip.

IsDate can be a tad bit slow if you validate several dates in a batch. The following code is much faster and performs the task quite well.


Code:

    Function MyIsDate(ByRef vsDate As String) As Boolean
        Dim sDateVerificationString As String = "\d{1,2}[ -/]\d{1,2}[ -/]\d{4}"

        If Regex.IsMatch(vsDate, sDateVerificationString) Then
            Try
                Dim a As Date = CDate(vsDate)
            Catch ex As Exception
                Return False
            End Try

' The following two lines of code are optional
            vsDate = Replace(vsDate, " ", "/")
            vsDate = Replace(vsDate, "-", "/")

            Return True

        Else
            Return False

        End If
    End Function


MEMBER FEEDBACK TO THIS TIP

The tip is quite nice, but in addition to its functionality, a two-sentence walkthrough on the match string would have been appreciated. Not everyone has used regular expressions extensively, and functions like these are a good way to improve one's familiarity. Also, while I could look it up and derive what the string is intended to do, the author clearly has done so already -- why not share the effort?

-- Bruce N.

**********************************************************************

    Requires Free Membership to View

    When you register, you'll begin receiving targeted emails from my team of award-winning writers. Our goal is to provide a unique online resource for developers, architects and development managers tasked with building and maintaining enterprise applications using Visual Basic, C# and the Microsoft .NET platform.

    Hannah Smalltree, Editorial Director

    By submitting your registration information to SearchWinDevelopment.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchWinDevelopment.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

Well, I would assume that one of the reasons this function is so much faster than the IsDate function is because the IsDate function allows for many more date formats than this function does. I suppose that this function would be fine if you were sure that the only dates that you'd be checking would have a format of:

"dd/mm/yyyy"
or
"dd-mm-yyyy"
or
"dd mm yyyy"

These are alternatives of switching the month and date while also allowing for only one digit for the month/day combo.

-- Steve L.

**********************************************************************

This is a pretty good example of using Regular Expressions. However, my question is, as per my knowledge, is it just going to check the well formness of the date? As such it will just check mm/dd/yyyy or dd/mm/yyyy.

I am not able to see any code that will validate for leap year and such possibilities. I would like to receive your opinion.

-- Pradeep G.

**********************************************************************

It doesn't take into account that there are only certain valid numbers that are dates and days. 99/99/9999 is not a valid date but would pass that regular expression.

-- Adam G.

Do you have comments of your own? Let us know.


This was first published in April 2004

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.