Different date conversion from string to date in VB.NET


In this article, we are going to talk about the different types of methods that can be used to convert a string with date type format into Date format in VB.NET. There are several inbuilt methods available in the Convert() class of the System package and Date class. Listed below are some of the methods used for this conversion.

 Convert.ToDateTime("Date String")
 DateTime.Parse("Date String")
 DateTime.ParseExact("Date String", "Format", System.IFormatProvider)
 DateTime.TryParse("Date String", New Date)

How to use Convert class for the conversion?

Convert class is available in System namespace and it is readily used to convert from a base type to another base type. In our example, we are converting string date into a date format. There are several methods available in the Convert class for the conversions. Few of them are:

 Convert.ToInt32(number)
 Convert.ToBoolean(boolean)
 Convert.ToString(string)
 Convert.ToChar(character)
 Convert.ToByte(number)

Convert.ToDateTime

By using ToDateTime() which takes a string argument, we are converting it to date format.

    Sub convertToDate()
        Try

            Dim currentDate As String = "03/03/2019"
            Dim convertedDate As DateTime = Convert.ToDateTime(currentDate)
            MessageBox.Show("Day: " & convertedDate.Day & " Month: " & convertedDate.Month & " Year: " & convertedDate.Year)

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

Output

Day: 3 Month: 3 Year: 2019

Using DateTime class for the conversion

There are mainly 3 methods in the DateTime class utilize for the string to date conversion. Parse() method only allows strings in the date format. Other than the date format in the strings, it is not allowed to be passed into the Parse() method.

DateTime.Parse

    Sub convertToDate()
        Try
       
        Dim stringDate As String = "2005-05-05"
        Dim convertedDate As DateTime = DateTime.Parse(stringDate)
        MessageBox.Show("Date: " & convertedDate.Day & " Month: " & convertedDate.Month & " Year: " & convertedDate.Year)

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

Output

Date: 5 Month: 5 Year: 2005

DateTime.ParseExact

ParseExact() is little bit different from the Parse() method. In the ParseExact(), we can set the format of the output. Hence, the string we are passing to the ParseExact() need to be in proper format always. This method is typically used, when the input string always has a predefined date format.

    
    Sub convertToDate()
        Try
        Dim stringDate As String = "2019-03-03 12:00 PM"
        Dim convertedDate As DateTime = DateTime.ParseExact(stringDate, "yyyy-MM-dd HH:mm tt", Nothing)

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

Output

3/3/2019 12:00:00 PM

Note: the function above will throw this error if the date string is not well formatted: The string was not recognized as a valid DateTime.

DateTime.TryParse

Last method available in the DateTime class is the TryParse() method. This method is the least used method out of the 3 methods defined in the DateTime class. One of the reasons for this is; TryParse() method doesn’t throw any exception when the conversion fails and for any missing data in the string, it fills them with default values. So it is not recommended to use this method unless there is an absolute necessity. TryParse() takes two arguments and the first one is the string that needs to be converted to the date format and the second argument is the instance of DateTime class which can hold the converted string.

    Sub convertToDate()
        Try

            Dim stringDate As String = "02/s03/2019 16:00:00"
            Dim resultDate As DateTime

            If DateTime.TryParse(stringDate, resultDate) Then
                MessageBox.Show("Converted: " + stringDate + " to " + resultDate)
            Else
                MessageBox.Show("Conversion Failed" + stringDate)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

Converted: 02/03/2019 16:00:00 to 2/3/2019 4:00:00 PM

Using CDate() for the conversion

Lastly, VB.NET has one of its default CDate() method to convert a string to date format. It converts valid time and date expression from string format to date format. It takes one argument as the input and returns the converted date in the same format as the input string.

    Sub convertToDate()
        Dim date1 As Date
        Dim date2 As Date
        Dim date3 As Date

        date1 = CDate("March 03, 2019") ' prints 03/03/2019
        date2 = CDate("12:27:00 PM") ' prints 12:27:00 PM
        date3 = CDate("March 03, 2019 12:27:00 PM") ' prints 03/03/2019 12:27:00 PM
        MessageBox.Show(date1 + Environment.NewLine + date2 + Environment.NewLine + date3)

    End Sub

Furthermore, we can use cultures in VB.NET to format the date into different types of formats available in each country as you wish.

Related Articles

Introduction to File handling methods in VB.NET

Introduction to DataAdapter in .NET

Sending Email Using Email Templates in ASP.NET

Tutorials Panel

About Tutorials Panel

programming tutorial and source code for Software Engineers and developers. https://twitter.com/TutorialsPanel

View all posts by Tutorials Panel →

Leave a Reply