Results 1 to 1 of 1

Thread: VB - Replacement for IsDate function

  1. #1

    Thread Starter
    Lively Member Flustor's Avatar
    Join Date
    Sep 2001
    Location
    A small hole in Birmingham
    Posts
    76

    VB - Replacement for IsDate function

    VB Code:
    1. Option Explicit
    2. '---------------------------
    3. 'Replacement IsDate function
    4. '---------------------------
    5.  
    6. 'Visual Basic's IsDate function falsely returns values such as 1.7 as true.
    7. 'This function resolves this issue.
    8.  
    9. Public Function ReplacementIsDate(ByVal sPotentialDate As String) As Boolean
    10. Dim varSplit        As Variant
    11. Dim bValidDay       As Boolean
    12. Dim bValidMonth     As Boolean
    13. Dim bValidYear      As Boolean
    14. Dim iStep           As Integer
    15.  
    16. ReplacementIsDate = False
    17. varSplit = Split("Blank-Blank", "-")
    18. If InStr(1, sPotentialDate, "\", vbTextCompare) > 0 Then
    19.     varSplit = Split(sPotentialDate, "\")
    20. End If
    21. If InStr(1, sPotentialDate, "/", vbTextCompare) > 0 Then
    22.     varSplit = Split(sPotentialDate, "/")
    23. End If
    24. If InStr(1, sPotentialDate, "-", vbTextCompare) > 0 Then
    25.     varSplit = Split(sPotentialDate, "-")
    26. End If
    27.  
    28. If UBound(varSplit) = 2 Then
    29.     'Validate Year
    30.     If IsNumeric(varSplit(2)) = True Then
    31.         If (CInt(varSplit(2)) < 3000) And (CInt(varSplit(2)) > 0) Then
    32.             bValidYear = True
    33.         End If
    34.     End If
    35.    
    36.     'Check for text month
    37.     'Exit function if both of the first two parts of the array are text
    38.     'i.e. May-May-2003
    39.     If (IsNumeric(varSplit(0)) = False) And (IsNumeric(varSplit(1)) = False) Then
    40.         Exit Function
    41.     End If
    42.    
    43.     'i.e. 31-12-2003
    44.     'If both day and month are numeric...
    45.     If (IsNumeric(varSplit(0)) = True) And (IsNumeric(varSplit(1)) = True) Then
    46.         'assuming the first part of the array is the month and the second the day...
    47.         If CInt(varSplit(0)) < 32 And CInt(varSplit(0)) > 0 Then
    48.             If CInt(varSplit(1)) < 13 And CInt(varSplit(1)) > 0 Then
    49.                 bValidDay = True
    50.                 bValidMonth = True
    51.             End If
    52.         End If
    53.         'assuming the first part of the array is the day and the second the month...
    54.         If CInt(varSplit(0)) < 13 And CInt(varSplit(0)) > 0 Then
    55.             If CInt(varSplit(1)) < 32 And CInt(varSplit(1)) > 0 Then
    56.                 bValidDay = True
    57.                 bValidMonth = True
    58.             End If
    59.         End If
    60.     End If
    61.    
    62.     'i.e. Jan/5/2003
    63.     'If the first part of the array is text then
    64.     If IsNumeric(varSplit(0)) = False Then
    65.         'If the first part of the array is a valid month then
    66.         If IsTextMonth(CStr(varSplit(0))) = True Then
    67.             If CInt(varSplit(1)) < 32 And CInt(varSplit(1)) > 0 Then
    68.                 bValidDay = True
    69.                 bValidMonth = True
    70.             End If
    71.         End If
    72.     End If
    73.    
    74.     'i.e. 29-May-1977
    75.     'If the second part of the array is text then
    76.     If IsNumeric(varSplit(1)) = False Then
    77.         'If the second part of the array is a valid month then
    78.         If IsTextMonth(CStr(varSplit(1))) = True Then
    79.             If CInt(varSplit(0)) < 32 And CInt(varSplit(0)) > 0 Then
    80.                 bValidDay = True
    81.                 bValidMonth = True
    82.             End If
    83.         End If
    84.     End If
    85.  
    86. End If
    87.  
    88. If (bValidDay = True) And (bValidMonth = True) And (bValidYear = True) Then
    89.     ReplacementIsDate = True
    90. End If
    91.  
    92. End Function
    93.  
    94. Public Function IsTextMonth(ByVal sPotentialMonth As String) As Boolean
    95. Dim bytStep         As Byte
    96. Dim sCurrentMonth   As String
    97.  
    98. sPotentialMonth = Trim(sPotentialMonth)
    99.  
    100. For bytStep = 1 To 12
    101.     sCurrentMonth = Format("28-" & bytStep & "-2000", "mmm")
    102.     If UCase(sCurrentMonth) = UCase(sPotentialMonth) Then
    103.         IsTextMonth = True
    104.         bytStep = 12
    105.     End If
    106. Next bytStep
    107. For bytStep = 1 To 12
    108.     sCurrentMonth = Format("28-" & bytStep & "-2000", "mmmm")
    109.     If UCase(sCurrentMonth) = UCase(sPotentialMonth) Then
    110.         IsTextMonth = True
    111.         bytStep = 12
    112.     End If
    113. Next bytStep
    114.  
    115. End Function
    Last edited by crptcblade; Jun 5th, 2003 at 06:14 AM.
    My Spidey senses are tingling!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width