Formatting DateTime using VB.NET

How to format DateTime to different formats.
  • 7280

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

            Console.WriteLine(format)

        Next

    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.

 

Code

Pattern

Format

Sample

"d"

Sort date

 

 

"D"

Long date

 

 

"f"

Full date time. Shorttime.

 

 

"F"

Full date time. Longtime.

 

 

"g"

Generate date time.Short time.

 

 

"G"

General date time.Long time.

 

 

"M", 'm"

Month/day.

 

 

"O", "o"

Round-trip date/time.

 

 

"R", "r"

RFC1123

 

 

"s"

Sortable date time.

 

 

"t"

Sort time.

 

 

"T"

Long time.

 

 

"u"

Universal sortabledate time.

 

 

"U"

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("----------------")

        Console.WriteLine("d Formats")

        Console.WriteLine("----------------")

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

        For Each Format As String In dateFormats

            Console.WriteLine(Format)

        Next

        Console.WriteLine("----------------")

        Console.WriteLine("D Formats")

        Console.WriteLine("----------------")

        For Each Format As String In dateFormats

            Console.WriteLine(Format)

        Next

 

        Console.WriteLine("----------------")

        Console.WriteLine("f Formats")

        Console.WriteLine("----------------")

        dateFormats = dob.GetDateTimeFormats("f"c)

        For Each Format As String In dateFormats

            Console.WriteLine(Format)

        Next

 

        Console.WriteLine("----------------")

        Console.WriteLine("F Formats")

        Console.WriteLine("----------------")

        dateFormats = dob.GetDateTimeFormats("F"c)

        For Each Format As String In dateFormats

            Console.WriteLine(Format)

        Next

 

    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.  

Console.WriteLine(dob.ToString("r"c));

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

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.