Problem solve Get help with specific problems with your technologies, process and projects.

IsDate replacement

This tip provides code to speed up the IsDate task.

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.

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

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.


Dig Deeper on Win Development Resources

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

TheServerSide.com

Close