Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleConvert a date string with known format into a Date
Keywordsdate, format, convert
CategoriesUtilities, Tips and Tricks
 
The CDate function uses the date format set by your computer's internationalization settings. That can be a problem if you want a program to run on many different computers.

This function takes date and format strings and returns a Date value. It splits the date at slash characters. Then using the three letters in the format string, it determines whether the characters between slashes should be a month, day, or year.

 
' Convert a date string into a Date value using a format.
' The format should be "ymd", "mdy", etc.
Private Function ToDate(ByVal date_string As String, ByVal _
    date_format As String) As Date
Dim date_parts() As String
Dim day_part As String
Dim month_part As String
Dim year_part As String

    date_parts = Split(date_string, "/")

    date_format = LCase$(date_format)
    Select Case Mid$(date_format, 1, 1)
        Case "d"
            day_part = date_parts(0)
        Case "m"
            month_part = date_parts(0)
        Case "y"
            year_part = date_parts(0)
    End Select
    Select Case Mid$(date_format, 2, 1)
        Case "d"
            day_part = date_parts(1)
        Case "m"
            month_part = date_parts(1)
        Case "y"
            year_part = date_parts(1)
    End Select
    Select Case Mid$(date_format, 3, 1)
        Case "d"
            day_part = date_parts(2)
        Case "m"
            month_part = date_parts(2)
        Case "y"
            year_part = date_parts(2)
    End Select

    ToDate = CDate(day_part & " " & MonthName(month_part) & _
        ", " & year_part)
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated