|
Function String2Date(strDate As String, strFmt As String) As Date
'
' Converts strings to dates depending on strFmt
' See Help on Format for what the various strings mean
' Comments use 27 May, 1993 as example date to show input text
'
If VarType(strDate) <> 8 Then
String2Date = Null
Exit Function
End If
Select Case strFmt
Case "MMDDYY", "MMDDYYYY" '052793 05271993
String2Date = CVDate(Left(strDate, 2) & "/" & Mid(strDate, 3, 2) & "/" & Mid(strDate, 5))
Case "DDMMYY", "DDMMYYYY" '270593 27051993
String2Date = CVDate(Mid(strDate, 3, 2) & "/" & Left(strDate, 2) & "/" & Mid(strDate, 5))
Case "YYMMDD" '930527
String2Date = CVDate(Mid(strDate, 3, 2) & "/" & Right(strDate, 2) & "/" & Left(strDate, 2))
Case "YYYYMMDD" '19930527
String2Date = CVDate(Mid(strDate, 5, 2) & "/" & Right(strDate, 2) & "/" & Left(strDate, 4))
Case "MM/DD/YY", "MM/DD/YYYY", "M/D/Y", "M/D/YY", "M/D/YYYY", "DD-MMM-YY", "DD-MMM-YYYY"
String2Date = CVDate(strDate)
Case "DD/MM/YY", "DD/MM/YYYY" '27/05/93 27/05/1993
String2Date = CVDate(Mid(strDate, 4, 3) & Left(strDate, 3) & Mid(strDate, 7))
Case "YY/MM/DD" '93/05/27
String2Date = CVDate(Mid(strDate, 4, 3) & Right(strDate, 2) & "/" & Left(strDate, 2))
Case "YYYY/MM/DD" '1993/05/27
String2Date = CVDate(Mid(strDate, 6, 3) & Right(strDate, 2) & "/" & Left(strDate, 4))
Case Else
String2Date = Null
End Select
End Function |
|