Wednesday, June 22, 2005

To get a date if we know the format there is no direct fuction in vb. below code helps you achieve this task.


Private Function getMyDate(ByVal date_string As String, ByVal date_format As String) As Date
Dim date_parts() As String, date_format_parts() As String
Dim day_part As String
Dim month_part As String
Dim year_part As String

date_parts = Split(date_string, "/")

If UBound(date_parts) = 0 Then
date_parts = Split(date_string, ".")
date_format = Replace(date_format, ".", "/")
End If
If UBound(date_parts) = 0 Then
date_parts = Split(date_string, "-")
date_format = Replace(date_format, "-", "/")
End If

date_format_parts = Split(date_format, "/")

Select Case Left(LCase(date_format_parts(0)), 1)
Case "d"
day_part = date_parts(0)
Case "m"
If Len(date_format_parts(0)) > 2 Then
month_part = date_parts(0)
Else
month_part = MonthName(date_parts(0))
End If
Case "y"
year_part = date_parts(0)
End Select
Select Case Left(LCase(date_format_parts(1)), 1)
Case "d"
day_part = date_parts(1)
Case "m"
If Len(date_format_parts(1)) > 2 Then
month_part = date_parts(1)
Else
month_part = MonthName(date_parts(1))
End If
Case "y"
year_part = date_parts(1)
End Select
Select Case Left(LCase(date_format_parts(2)), 1)
Case "d"
day_part = date_parts(2)
Case "m"
If Len(date_format_parts(2)) > 2 Then
month_part = date_parts(2)
Else
month_part = MonthName(date_parts(2))
End If
Case "y"
year_part = date_parts(2)
End Select
getMyDate = CDate(day_part & " " & month_part & _
", " & year_part)
End Function

Web Hosting Cheap PHP Plans Also at For free flags of the world