How to Convert DateTime to String

Convert a DateTime to a string or other data types.
  • 2323

Converting DateTime 
 

The DateTime structure is full with self-explanatory conversion methods that convert a DateTime to a specified type. These methods are ToBinary, ToFileTime, ToLocalTime, ToLongDateString, ToLongTimeString, ToOADate, ToShortDateString, ToShortTimeString, ToString, and ToUniversalTime. 
 

The code snippet in Listing 16 uses some of these methods to convert a DateTime to specified types. 
 

    Private Sub ConvertDateTimeToTypes()

        Dim dob As DateTime = New DateTime(2002, 10, 22)

        Console.WriteLine("ToString: " + dob.ToString())

        Console.WriteLine("ToBinary: " + dob.ToBinary())

        Console.WriteLine("ToFileTime: " + dob.ToFileTime())

        Console.WriteLine("ToLocalTime: " + dob.ToLocalTime())

        Console.WriteLine("ToLongDateString: " + dob.ToLongDateString())

        Console.WriteLine("ToLongTimeString: " + dob.ToLongTimeString())

        Console.WriteLine("ToOADate: " + dob.ToOADate())

        Console.WriteLine("ToShortDateString: " + dob.ToShortDateString())

        Console.WriteLine("ToShortTimeString: " + dob.ToShortTimeString())

        Console.WriteLine("ToUniversalTime: " + dob.ToUniversalTime())

    End Sub

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.