How to Use the Convert Function for the Date Format

How to Use the Convert Function for the Date Format thumbnail
The convert function is a part of Visual Basic.

The .NET Visual Basic programming language provides developers with an internal "Convert()" function that changes the format of a string to a date. Data types are part of any development environment, so using the convert function allows you to implement date-specific functions on the string. Programmers can implement the Visual Basic convert function on any string that contains a day, month and year.

Instructions

    • 1

      Create the Visual Basic (VB) variables used to contain the string and converted date. The following code creates your VB variable:
      Dim strDate As String
      Dim dtDate As DateTime

    • 2

      Enter a value for the string variable. The value in the string must be a date or the convert function returns an error. In this example, a "short date" is created in the string variable. The code below is used to assign a value to the string:
      strDate = "3/1/2010"

    • 3

      Implement the convert function and assign it to the appropriate date variable. A converted date must be assigned to a variable with the date data type. This is why the extra date variable was defined in Step 1. The following code uses the convert function for the date:
      dtDate = Convert.ToDateTime(strDate)
      This code transfers and converts the string variable to the date variable. Now that the string is converted, you can use date-specific functions on it. For instance, the code below adds a day to the current date:
      dtDate = dtDate.AddDays(1)

Related Searches:

References

Resources

  • Photo Credit computer image by martini from Fotolia.com

Comments

You May Also Like

Related Ads

Featured