Recipe 7.10. Formatting Dates and Times


Problem

You want to format a date or time for output using some standard or custom format.

Solution

Sample code folder: Chapter 07\FormatDateTime

Use one of the single-letter format options, or set up a custom format to convert the date or time as desired.

Discussion

The following code displays most of the standard formats available for converting dates and times to strings, plus a sampling of what the custom formatting options can do:

 Dim rightNow As Date = Now Dim result As New System.Text.StringBuilder result.AppendLine("""Now""…") result.AppendLine() ' ----- Use some of the built-in Date properties to '       format the date in predefined ways. result.Append("ToString: ").AppendLine(rightNow.ToString) result.Append("ToLongDateString: ") result.AppendLine(rightNow.ToLongDateString) result.Append("ToShortDateString: ") result.AppendLine(rightNow.ToShortDateString) result.Append("ToLongTimeString: ") result.AppendLine(rightNow.ToLongTimeString) result.Append("ToShortTimeString: ") result.AppendLine(rightNow.ToShortTimeString) result.Append("ToUniversalTime: ") result.AppendLine(rightNow.ToUniversalTime) result.AppendLine() ' ----- Use format specifiers to control the   date display. result.Append("d: ").AppendLine(rightNow.ToString("d")) result.Append("D: ").AppendLine(rightNow.ToString("D")) result.Append("t: ").AppendLine(rightNow.ToString("t")) result.Append("T: ").AppendLine(rightNow.ToString("T")) result.Append("f: ").AppendLine(rightNow.ToString("f")) result.Append("F: ").AppendLine(rightNow.ToString("F")) result.Append("g: ").AppendLine(rightNow.ToString("g")) result.Append("G: ").AppendLine(rightNow.ToString("G")) result.Append("M: ").AppendLine(rightNow.ToString("M")) result.Append("R: ").AppendLine(rightNow.ToString("R")) result.Append("s: ").AppendLine(rightNow.ToString("s")) result.Append("u: ").AppendLine(rightNow.ToString("u")) result.Append("U: ").AppendLine(rightNow.ToString("U")) result.Append("y: ").AppendLine(rightNow.ToString("y")) result.AppendLine().AppendLine() ' ----- Use custom format specifiers, which provide '       more flexibility than the single-letter formats. result.Append("dd: ").AppendLine(rightNow.ToString("dd")) result.Append("ddd: ").AppendLine(rightNow.ToString("ddd")) result.Append("dddd: ").AppendLine(rightNow.ToString("dddd")) result.Append("HH:mm:ss.fff z: ") result.AppendLine(rightNow.ToString("HH:mm:ss.fff z")) result.Append("yy/MM/dd g: ") result.AppendLine(rightNow.ToString("yy/MM/dd g")) MsgBox(result.ToString) 

The output of this code appears in Figure 7-10.

The first group of lines shows string conversions provided by specific members of the Date object. You'll probably find these common formats sufficient for most purposes.

The second group of lines shows the single-letter predefined formats, which provide even more options. These letters don't appear in the IntelliSense pop ups, so if you do a lot of formatting along these lines, you might want to make a list for your own reference.

Custom date output is provided by strings of specifically defined characters that format parts of the Date appropriately. A sampling is shown in this code, and the Visual Studio online help documents all available formats.

This recipe's sample code uses the Date's ToString() method exclusively to format the dates and times. However, there are other objects that support the IFormattable interface, which provides very similar formatting capabilities. Specifically, the String.Format() shared method provides similar formatting capabilities in its braces-defined format parameters.

Figure 7-10. A sampling of predefined and custom formats available for formatting Date variables to strings


For example, here's one of the lines from the previous example code:

 result.Append("d: ").AppendLine(rightNow.ToString("d")) 

This same line of output can also be formatted using String.Format():

 result.Append(String.Format( _    "d: {0:d}{1}", rightNow, vbNewLine)) 

In this case, the {0:d} format parameter provides the same formatting instruction as the d string parameter in the ToString() method. These two lines demonstrate very different syntax, but they produce the same results.

See Also

For details on all predefined and custom format strings available in .NET, access the "formatting types" entry in the Visual Studio online help documentation.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net