
Originally Posted by
inoka
Hello Leith Ross,
Could you please help me to do the previous same functionality for the date format "DD/MM/YYYY".
Hi there
7 years late on this one, but I needed to do the same thing. I've found a way to solve this, but it may not necessarily be completely bullet-proof.
I've taken most of the code, but you'll see I do some specific checks. If the format is dd/mm/yyyy I expect, at least, that the minimum length is 8, and that there can be found exactly 2 instances of the "/" character. If at least that is true, then we're able to do the remaining checks in the code as written by Leith Ross.
Function ValidateDate(ByVal DateCode As Variant)
Dim D As Integer
Dim LeapYear As Boolean
Dim M As Integer
Dim Result As Boolean
Dim Y As Integer
Dim c As Integer
c = 1
Dim countSlash As Integer
countSlash = 0
Do While c <> Len(DateCode)
If InStr(c, DateCode, "/") = 0 Then
c = Len(DateCode)
Else
c = InStr(c, DateCode, "/") + 1
countSlash = countSlash + 1
End If
Loop
If (Len(DateCode) < 8 Or countSlash <> 2) And Not IsEmpty(DateCode) Then
Exit Function
End If
If IsEmpty(DateCode) Then
Y = 1900
M = 1
D = 1
Else
Y = CInt(Right(DateCode, 4)) 'CInt(Left(DateCode, 4))
M = CInt(Mid(DateCode, InStr(DateCode, "/") + 1, InStr(InStr(DateCode, "/") + 1, DateCode, "/") - InStr(DateCode, "/") - 1)) 'CInt(Mid(DateCode, 5, 2))
D = CInt(Left(DateCode, InStr(DateCode, "/") - 1)) 'CInt(Right(DateCode, 2))
End If
If Y < 1900 Then
ValidateDate = False: Exit Function
End If
LeapYear = (Y Mod 100 <> 0 And Y Mod 4 = 0) Or (Y Mod 400 = 0)
Select Case M
Case 1, 3, 5, 7, 8, 10, 12
If D >= 1 And D <= 31 Then
Result = True
End If
Case 2
If LeapYear Then
If D >= 1 And D <= 29 Then Result = True
Else
If D >= 1 And D <= 28 Then Result = True
End If
Case 4, 6, 9, 11
If D >= 1 And D <= 30 Then
Result = True
End If
End Select
Finished:
ValidateDate = Result
End Function
Bookmarks