ARTICLE

Formatting DateTime using VB.NET

Posted by John Arcadella Articles | Visual Basic 2010 January 26, 2011
How to format DateTime to different formats.
 
Reader Level:

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

 

Listing 13

 


Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor