Formatting DateTime using VB.NET

How to format DateTime to different formats.
  • 8835

Formatting DateTime 

I have to admit, folks at Microsoft have done a great job by taking care of DateTime formatting problems. Now you can format a DateTime to any kind of string format you can imagine. 

The GetDateTimeFormats method returns all possible DateTime formats for the current culture of a computer. The code snippet in Listing 10 returns an array of strings listed all possible formats. 

    Private Sub GetAllDateTimeFormats()

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

        Dim dateFormats() As String = dob.GetDateTimeFormats()

        For Each format As String In dateFormats



    End Sub 

Listing 10


The GetDateTimeFormats method also has an overloaded for that takes a format specifier as a parameter and converts a DateTime to that format. It is very important to understand the DateTime format specifiers to get the desired formats. Table 1 summarizes the formats and their codes.







Sort date




Long date




Full date time. Shorttime.




Full date time. Longtime.




Generate date time.Short time.




General date time.Long time.



"M", 'm"




"O", "o"

Round-trip date/time.



"R", "r"





Sortable date time.




Sort time.




Long time.




Universal sortabledate time.




Universal full datetime.



"Y", "y"

Year month.




Table 1

The code snippet in Listing 11 uses some of the DateTime format specifiers.


    Private Sub FormatDateTime()


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

        ' DateTime Formats: d, D, f, F, g, G, m, o, r, s, t, T, u, U,


        Console.WriteLine("d Formats")


        Dim dateFormats() As String = dob.GetDateTimeFormats("d"c)

        For Each Format As String In dateFormats




        Console.WriteLine("D Formats")


        For Each Format As String In dateFormats





        Console.WriteLine("f Formats")


        dateFormats = dob.GetDateTimeFormats("f"c)

        For Each Format As String In dateFormats





        Console.WriteLine("F Formats")


        dateFormats = dob.GetDateTimeFormats("F"c)

        For Each Format As String In dateFormats




    End Sub


Listing 11


We can also format a DateTime by passing the format specifier in the ToString() method of DateTime. The code snippet in Listing 12 formats a DateTime using ToString() method.  


Listing 12 

The code snippet in Listing 13 uses some of the DateTime format specifiers within the ToString() method to format a DateTime. 

    Private Sub FormatUsingToString()

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    End Sub


More Articles

© 2020 DotNetHeaven. All rights reserved.